cgiffard / Behaviour-Assertion-Sheets

CSS-like declarative DSL for web integration testing.
http://bas.cgiffard.com/
138 stars 8 forks source link

How to test if the `content-type` header of a given URL matches e.g. `text/html`? #11

Closed mathiasbynens closed 11 years ago

mathiasbynens commented 11 years ago

Is it possible to test if the content-type header of a given URL contains, say, text/html, and matches /utf-8/i too?

I was trying this:

@page (path = /index.php) {
  status-code: 200;
  content-type: contains('text/html'), /utf-8/i;
}

…but it seems it doesn’t work that way:

    1 failed assertions:
        'content-type' expects contains('text/html'), /utf-8/i: Component test 'contains' failed against input 'text/html'.
cgiffard commented 11 years ago

Hah! That sounds like a rather embarrassing bug. :) If I get an opportunity today, I'll get right on that! Thanks for filing an issue.

I suspect it isn't pulling the quotes off or something. I'll go have a good poke around.

cgiffard commented 11 years ago

Hi again. Just doing some debugging - is it possible for you you to post a link to the page that you were scanning at the time? (Or one which exhibits the same effect.)

mathiasbynens commented 11 years ago

Sure: http://mathiasbynens.be/demo/bas-issue-11

Here’s how you can reproduce the issue:

sheet.bas:

@page (path = /demo/bas-issue-11) {
  status-code: 200;
  content-type: contains('application/json'), /utf-8/i;
}

@page (path = /demo/bas-issue-11?callback=foo) {
  status-code: 200;
  content-type: contains('application/javascript'), /charset=utf-8/i;
}

Then run:

bas -v -s sheet.bas http://mathiasbynens.be/demo/bas-issue-11{,?callback=foo}
cgiffard commented 11 years ago

Cool, thanks. I've tracked it down to the statement parser - somehow I introduced a bug recently which is preventing the proper testing of multi-component assertions. I'm writing a bunch of tests to make sure this is caught in future - will push an update soonish.

Sorry for the inconvenience!

mathiasbynens commented 11 years ago

Thanks, and thank you for BAS — it’s immensely useful! I’m using it to help automate grading my students’s assignments.

cgiffard commented 11 years ago

Thanks, glad it's a help. :)

OK - so I'm pretty sure I've fixed your problem - the issue related to parsing of assertions, which I'd broken somehow, and should now be far more stable. The latest bas 0.11 is up on npm, so you can try it out and make sure it's running OK on your end too.

➭  bas -v -s sheet.bas http://mathiasbynens.be/demo/bas-issue-11{,?callback=foo}
Using behaviour assertion sheet: sheet.bas
Commencing initial data request.
Spooled URLs are:
    http://mathiasbynens.be/demo/bas-issue-11
    http://mathiasbynens.be/demo/bas-issue-11?callback=foo
Testing resource: http://mathiasbynens.be/demo/bas-issue-11
    Commencing BAS test suite
    Starting test group: @page: (path = /demo/bas-issue-11)
            Testing assertion status-code: 200
                ✔  status-code: 200
            Testing assertion content-type: contains('application/json'), /utf-8/i
                ✔  content-type: contains('application/json'), /utf-8/i
    BAS test suite completed with no errors.
Testing resource: http://mathiasbynens.be/demo/bas-issue-11?callback=foo
    Commencing BAS test suite
    Starting test group: @page: (path = /demo/bas-issue-11?callback=foo)
            Testing assertion status-code: 200
                ✔  status-code: 200
            Testing assertion content-type: contains('application/javascript'), /charset=utf-8/i
                ✔  content-type: contains('application/javascript'), /charset=utf-8/i
    BAS test suite completed with no errors.

Test batch completed. (2 requests)
No assertion failures encountered over batch.

The code in question now has pretty good test coverage, so I'm hoping something like this won't happen again. Apologies for the inconvenience!

mathiasbynens commented 11 years ago

Working perfectly now! Thank you!