Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL Wildcards for hook matching #67

Open
ProLoser opened this issue Feb 1, 2016 · 2 comments
Open

URL Wildcards for hook matching #67

ProLoser opened this issue Feb 1, 2016 · 2 comments

Comments

@ProLoser
Copy link
Collaborator

ProLoser commented Feb 1, 2016

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();
});
@amarseillan-zz
Copy link

👍

@runejuhl
Copy link

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.

suelje added a commit to eXa-online/abao that referenced this issue Mar 2, 2016
suelje added a commit to eXa-online/abao that referenced this issue Mar 2, 2016
suelje added a commit to eXa-online/abao that referenced this issue Mar 2, 2016
suelje added a commit to eXa-online/abao that referenced this issue Mar 2, 2016
avdv pushed a commit to eXa-online/abao that referenced this issue Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants