How do I verify pacts against an API that requires an auth token?

前端 未结 2 1627
不思量自难忘°
不思量自难忘° 2021-01-21 16:24

I\'m using the Pact gem (and loving it!) for my contract test suite. The API service I\'m testing requires an authorization token for all requests.

I know how to genera

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 16:47

    You actually don't have to use a real token for each pact interaction unless you really want/need to.

    Normally for that kind of stuff, I just create a regex to be used on the header to validate certain rules while keeping it 'open'. In my node project (which uses the Ruby binary in the back), I created these 2 utilities functions to create objects with a pattern and another for a object minimum equal:

    function term(matcher, generate) {
        if ((typeof matcher === 'undefined') || (typeof generate === 'undefined')) {
          throw 'Matcher and Generate arguments must be specified to use Term';
        }
        return {
          "json_class": "Pact::Term",
          "data": {
            "generate": generate,
            "matcher": {
              "json_class": "Regexp",
              "o": 0,
              "s": matcher
            }
          }
        };
      }
    
      function somethingLike(value) {
        return {
          "json_class": "Pact::SomethingLike",
          "contents": value
        };
      }
    

    You can then use it in your DSL definition like so:

    mockService
          .given('a form')
          .uponReceiving('a GET request with a valid auth')
          .withRequest('get', '/', term('^Bearer (?!null$).+$', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ'))
          .willRespondWith({
            status: 200,
            headers: {'Content-Type': 'application/json;charset=utf-8'},
            body: {worked:true}
          });
    

    The 'term' utility has a regex as the first parameter and then an example (that should match the first) of what to use during the test.

    I know that this needs to be expanded better within Pact itself to make it simpler to use. I hope this helps.

提交回复
热议问题