abao icon indicating copy to clipboard operation
abao copied to clipboard

URL Wildcards for hook matching

Open ProLoser opened this issue 9 years ago • 2 comments

I have a long api endpoint: /browse/items/{browse_type}/filter/{filter_type}/sort/{sort_method}/offset/{offset}/size/{size}: and I can query it by doing abao browse.raml /browse but in my hooks just using /browse will not match this endoint, instead I must specify the full endpoint lenght.

It would be nice if I could do either /browse/* or if the hooks used substring matches to figure out when they should be applied.

Example:

hooks.before('GET /browse/* -> 200', function(test, done) {
  test.request.query = { ... };
  done();
});

ProLoser avatar Feb 01 '16 22:02 ProLoser

:+1:

amarseillan-zz avatar Feb 16 '16 02:02 amarseillan-zz

I did start implementing RegExp matching hooks at some point, but for some reason it turned out awkward -- as I remember it, it had something to do with the manner in which the test cases are added. Part of my idea was to have the tests do things in order and keep state in-between, so my tests would be able to e.g. authenticate -> GET / using session -> POST /password/change using session. My checkout is from late November, so I might be remembering wrong :)

Anyway, until this is implemented, what you can do is create a beforeEach or afterEach hook that checks the request path:

{beforeEach, after} = require 'hooks'

makeAuth = (username, password) ->
  return (new Buffer "#{username}:#{password}").toString 'base64'

username = 'runejuhl'
password = 'testpass'
auth = (new Buffer "#{username}:#{password}").toString 'base64'

# This endpoint generates a new password and returns it in the response. If we
# update the global variable `password`, it'll be used on subsequent requests.
after 'POST /user/{email}/password/generate -> 200', (test, done) ->
  password = test.response.body.password
  console.log 'new pass', password
  done()

beforeEach (test, done) ->
  console.log 'before', test.request.path
  console.log 'pass is', password

  # Don't use Basic auth for endpoints starting with /public
  if /^\/public\//.test(test.request.path)
    done()

  auth = makeAuth(username, password)
  test.request.headers['Authorization'] = "Basic #{auth}"
  done()

@ProLoser : For your case, you could match on /^\/browse\/. If you need more than a few tests, I'd put the regexes and accompanying functions in a map and run all functions that match the regex.

runejuhl avatar Feb 24 '16 05:02 runejuhl