Hello , quick question for provider state set up, ...
# general
h
Hello , quick question for provider state set up, I have tried to send request to deployed test server(provider) for testing v3 beta but it throw an error(Response for preflight has invalid HTTP status code 405) https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request is the pact framework use a headless browser to send request? lol
t
It should only generate preflight if the consumer told it to. Is cors set to true in your consumer test?
Can you share the actual error you're getting? Is this from the state setup or the actual endpoint?
h
is the CORS set from consumer define in interaction header?
Copy code
headers: {
    "Content-Type": "application/json; charset=utf-8"
  },
if so, no we dont
it is from state setup, we had some ssl issue with local provider docker, so we just want to try out V3 beta to test env(staging) to point out it works as expected or not lol
t
No. That looks like it's pact-js - if so, cors is set on the
Pact
object when you create it.
I'm not sure how it is set in the beta. If you're not setting it at all, this is definitely a bug.
h
I am using
jest-pact
so it does magic for me? lol I did not set anything yet
maybe I will try to change
jest-pact
version to point out beta
t
jest-pact doesn't set cors by default
you won't be able to use it with the beta, though. There is a beta release of that, too
h
https://github.com/pact-foundation/jest-pact/blob/pact-js-v3/README.md yeap I found that, I will try and get back to you lol but if jest-pact doesnt set CORS, hum ….. I can downgrade to V2 and try same step to confirm
t
Can you share the actual error you're getting - It sounds like it might be a bug in the rust core.
ideally the whole log, please
h
yeap I need to go out now, for pick up πŸ™‚ I will get back to you later ! thanks
kids 1
m
I’d be interested to understand why you need CORS pre flights at all for your Pact test. This should be running in a Node process (V3 doesn’t support non-node environments to my knowledge) so there is no need for CORS
What HTTP client are you using?
t
Also, you're not using jest-pact at the provider side, right? It is only for consumer tests.
h
Copy code
console.error
    Error: Error: Response for preflight has invalid HTTP status code 405
        at Object.dispatchError (/app/app/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:63:19)
        at EventEmitter.<anonymous> (/app/app/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:655:18)
        at EventEmitter.emit (node:events:549:35)
        at Request.<anonymous> (/app/app/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:370:16)
        at Request.emit (node:events:537:28)
        at Request._processResponse (/app/app/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:228:12)
        at ClientRequest.<anonymous> (/app/app/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:101:12)
        at Object.onceWrapper (node:events:652:26)
        at ClientRequest.emit (node:events:537:28)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:642:27) undefined

      at VirtualConsole.<anonymous> (../node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
      at Object.dispatchError (../node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:66:53)
      at EventEmitter.<anonymous> (../node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:655:18)
      at Request.<anonymous> (../node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:370:16)

[2022-07-07T06:47:35Z ERROR pact_verifier] Provider state change for 'A new object will be created' has failed - MismatchResult::Error("Invalid status code: 500", Some("217f8b60aad7530b2fd3dc48dd723abba5fe8674"))
This is the error I got not sure it helps lol
Copy code
"interactions": [
    {
      "description": "a deleteObject request",
      "providerStates": [
        {
          "name": "A new object will be created"
        }
      ],
      "request": {
        "body": {
          "query": "\n  mutation DeleteObjectMutation($id: Int!) {\n    deleteObject(id: $id) {\n      id\n    }\n  }\n",
          "variables": {
            "id": 1
          }
        },
        "generators": {
          "body": {
            "$.variables.id": {
              "expression": "${objectId}",
              "type": "ProviderState"
            }
          }
        },
        "headers": {
          "Content-Type": "application/json"
        },
        "matchingRules": {
          "body": {
            "$.variables.id": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "type"
                }
              ]
            }
          }
        },
        "method": "POST",
        "path": "/api/v2/graphql"
      },
      "response": {
        "body": {
          "data": {
            "deleteObject": {
              "id": 1
            }
          }
        },
        "headers": {
          "Content-Type": "application/json"
        },
        "matchingRules": {
          "body": {
            "$.data.deleteObject.id": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "type"
                }
              ]
            }
          },
          "header": {}
        },
        "status": 200
      }
    }
  ]
contract
Copy code
stateHandlers: {
      'A new object will be created': {
        setup: async () => {
          const objectId = await createObject()
          return {
            objectId
          }
        }
      }
    }

const createObject = async (): Promise<number> => {
  const body = {
    query: "use mutation to create an object",
    variables: {}
  }
  try {
    const res = await <http://client.post|client.post>("/api/v2/graphql", body)
    return res.data.id
  } catch (e) {
    console.log(e)
    throw e
  }
}
provide state
t
That looks like it's not coming from Pact. How is your state change implemented? It looks like the state change is failing
That call in the state change is what is failing
h
yeap we tried to use exist provider side call to create
send api request to create test data
t
Well, that API request is sending a preflight that the server is rejecting
h
ok good to know thanks Timothy, let me try api request without any pact connection πŸ™‚
Hello Timothy, I have tested without pact (direct request to test server) it works as expected lol this is provider side option
Copy code
export const createOptions = (providerName: string)=> {
  return {
    provider: providerName,
    providerBaseUrl: `${providerBaseUrl}`,
    pactBrokerUrl: `${pactBrokerUrl}`,
    pactBrokerToken: `${pactBrokerToken}`,,
    consumerVersionSelectors:[{tag:'provider-status', latest: true}],
    providerVersionTags: ['provider-status'],
    providerVersion: 'provider-status',
    enablePending: true,
    validateSSL: false,
    disableSslVerification: true,
   
    stateHandlers: {
      'A new link will be created': {
        setup: async () => {
          const linkId = await createLinkInMonolith()
          return {
            linkId
          }
        },
        teardown: async () => {

        }
      }
    }
  }
}
we did not set
Copy code
providerStatesSetupUrl:
we got
stateHandlers
to send post request(axios) to server
just pure request using Axios without pact I can send request to server and resoponse as expected lol
t
just pure request using Axios
Yes, this is to be expected. The problem is in the
<http://client.post|client.post>()
call - so the question isn't "what happens if I don't use pact and implement a client myself?", the question is what happens when I do:
Copy code
const body = {
    query: "use mutation to create an object",
    variables: {}
  }
  try {
    const res = await <http://client.post|client.post>("/api/v2/graphql", body)
    return res.data.id
  } catch (e) {
    console.log(e)
    throw e
  }
without pact. (I can say confidently that it will fail - pact is not the problem here)
Specifically, the errors you're getting are from an xhr library, which Pact doesn't use. So Pact is definitely not the source of the problem.
h
yeap without pact, it works lol
Copy code
const axios = require('axios').default

const client = axios.create({
    baseURL: "my url",
    timeout: 10000,
    headers: {
        "Authorization": `Bearer token`,
        "Content-Type": "application/json"
    }
})

const body = {
    query: "mutation",
    variables: {
        accountId: 0000
    }
}

async function main() {
    try {
        const res = await <http://client.post|client.post>("/api/v2/graphql", body)
        console.log(res.data.data.addLink.id)
    } catch (e) {
        console.log(e)
        throw e
    }
}

main()
maybe I did not set
providerStatesSetupUrl:
is that the issue?
t
I think the issue is that code will finish before the client call is made.
πŸ€” 1
also you're sending a different request
h
different request
sorry what do you mean?
t
The body in your working example is different from the body in your failing example.
I'm afraid I can't help you debug your code. But I can answer your pact question:
maybe I did not set providerStatesSetupUrl: is that the issue?
No. You set either one of
stateHandlers
or
providerStateSetupUrl
, not both.
πŸ‘ 1
h
ahhh failing example(old shared one) was I modified it for sharing, the axios one (the latest shared one) and failed one are same πŸ™‚
I tested same request with pact, without pact!
code will finish before the client call is made
any tip to everything aligned?
t
Well, the failure isn't coming from pact.
πŸ‘ 1
h
Hello Timothy, I got it πŸ™‚ it was all becuz of jest config setup lol
Copy code
testEnvironment: "jest-environment-jsdom-sixteen",
this one causes issue, now all fixed and working as expected πŸ™‚
thanks for your Advice ! cheers