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

[V3] Using fromProviderState on body doesn't work properly #633

Closed
3 of 5 tasks
dpakach opened this issue Mar 23, 2021 · 8 comments
Closed
3 of 5 tasks

[V3] Using fromProviderState on body doesn't work properly #633

dpakach opened this issue Mar 23, 2021 · 8 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@dpakach
Copy link

dpakach commented Mar 23, 2021

Software versions

Please provide at least OS and version of pact-js

  • OS: ubuntu 20.04
  • Consumer Pact library: "10.0.0-beta.33"
  • Provider Pact library:
  • Node Version: v12.21.0

Issue Checklist

Please confirm the following:

  • I have upgraded to the latest
  • I have the read the FAQs in the Readme
  • I have triple checked, that there are no unhandled promises in my code
  • I have set my log level to debug and attached a log file showing the complete request/response cycle
  • For bonus points and virtual high fives, I have created a reproduceable git repository (see below) to illustrate the problem

Expected behaviour

fromProviderState on Body should return proper body
eg.

 provider
      .given("set id", {id: "42"})
      .uponReceiving("a request to get the plain data")
      .withRequest({
        method: "GET",
        path: MatchersV3.fromProviderState("/data/${id}", "/data/42"),
      })
      .willRespondWith({
        status: 200,
        headers: { "Content-Type": 'text/plain; charset=utf-8' },
        body: MatchersV3.fromProviderState("data: testData, id: ${id}", "data: testData, id: 42")
   })

    return provider.executeTest(async (mockserver) => {
      transactionService.setAccountServiceUrl(mockserver.url)
      return transactionService
        .getText(42)
        .then((result) => {
          console.log(result.data)
          expect(result.data).to.equal("data: testData, id: 42")
        })
    })
  })

Actual behaviour

The body returned is not string, rather a pact object

 Expected value   "data: testData, id: 42"
    Received:
      {"expression": "data: testData, id: ${id}", "pact:generator:type": "ProviderState", "pact:matcher:type": "type", "value": "data: testData, id: 42"}
    

Steps to reproduce

See the bug reproduced in dpakach#2
Test on https://github.com/dpakach/pact-js/pull/2/files#diff-656f935c93a4d4ea910cb4961f3463c434eb6ea7cbe8d51e2c7b0b3fd4c5ae0cR101

@dpakach dpakach added the bug Indicates an unexpected problem or unintended behavior label Mar 23, 2021
@individual-it
Copy link
Contributor

fromProviderState returns an object https://github.com/pact-foundation/pact-js/blob/feat/v3.0.0/src/v3/matchers.ts#L451-L455
but maybe we need something like

pact-js/src/v3/matchers.ts

Lines 404 to 417 in 45bb7c4

if (basePath == null) {
return {
"pact:matcher:type": "regex",
"pact:generator:type": "MockServerURL",
regex: regexpr + ")$",
value: example,
example,
}
} else {
return {
"pact:matcher:type": "regex",
regex: regexpr + ")$",
value: example,
}

@individual-it
Copy link
Contributor

it's not limited to fromProviderState, I cannot get any matcher working in the body response

@individual-it
Copy link
Contributor

adding body = body.value after https://github.com/pact-foundation/pact-js/blob/feat/v3.0.0/src/v3/pact.ts#L198 does solve it

@uglyog
Copy link
Member

uglyog commented Mar 26, 2021

Actually, I think this is due to the content type not being JSON, so it not knowing to process the intermediate format

@individual-it
Copy link
Contributor

@uglyog you are right setting the headers in the response to { "Content-Type": 'application/json; charset=utf-8' }
make the test pass.
But in this case the content between provider & consumer is not JSON, but as I understand you we need to declare the content to be JSON for pact-js to use it correctly

@uglyog
Copy link
Member

uglyog commented Mar 26, 2021

Sorry, I wasn't saying to do that to fix it, but rather, that is the cause of the issue.

I also can't understand why we are converting the bodies to JSON (https://github.com/pact-foundation/pact-js/blob/feat/v3.0.0/src/v3/pact.ts#L171), when we can just pass them through. I'm going to try change that, so we don't need to use the content type to know if we need to process the body.

@individual-it
Copy link
Contributor

should be fixed by #672

@dpakach dpakach closed this as completed May 31, 2021
@mefellows
Copy link
Member

v10.0.0-beta.36 is on the way out now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants