Hi everyone, I'm having an issue running a test t...
# pact-js
q
Hi everyone, I'm having an issue running a test that should be passing (and does on 10.0.0.beta54) after I've upgraded to 10.0.0.beta56. The issue is around the content-type header, and the error is
Expected body with content type application/json but was image/jpeg
. Can anyone please help?
here's my code:
Copy code
const { pactWith } = require("jest-pact/v3");
const { MatchersV3 } = require("@pact-foundation/pact/v3");
const mockPactServerClient = require("../../utility/mockPactServer");
const fs = require("fs");

const { string } = MatchersV3;

pactWith(
	{
		consumer: "bulk-upload",
		provider: "upload-provider",
		logLevel: 'INFO',
		port: 8081,
	},
	(interaction) => {
		const fileBody = Buffer.from(fs.readFileSync("pikachu.png")).toString("base64");
		interaction("initiateUpload", ({provider, execute}) => {
				beforeEach(() =>
					provider
						.uponReceiving("Initiate Upload")
						.withRequest({
							method: "PUT",
							path: '/bar/foo',
							headers: {
								Authorization: string("headerbar"),
								Accept: 'image/jpeg',
								'Content-Type': 'image/jpeg',
							},
							body: fileBody
						})
						.willRespondWith({
							status: 201
						})
				);
				execute("Content-type bug test", () => {
				return mockPactServerClient.put(
					'/bar/foo',
					fileBody,
					{
						headers: {
							Authorization: "Bearer Auth",
							Accept: 'image/jpeg',
							"Content-Type": "image/jpeg",
						},
					}
				);
			});
			}
		)
	}
	);
what doesn't make sense is that I'm specifying the content type as
"image/jpeg"
everywhere, but it's still looking for
application/json
?
here's the error
Copy code
● Pact between bulk-upload and upload-provider › with 30000 ms timeout for Pact › initiateUpload › Content-type bug test

    Test failed for the following reasons:

      Mock server failed with the following mismatches:

    	0) The following request was incorrect: 

            	PUT /bar/foo
            
    			 1.0 Expected body with content type application/json but was image/jpeg
m
try setting the
contentType
property on the request and response bodies. That is part of the API I’m still looking at addressing. We’re in an awkward state where I’d like to continue support the JSON-like setup above, but it means certain options aren’t forced to be set. I’d prefer a type-state builder style API, but that won’t play nice with Jest Pact etc.
q
that helped!
However, I'm running into another issue now with another test
I have a custom contentType that I need to match, and whenever it's used i get the same error as above. The contentType is like this:
application/foo.bar.bulk-transfer+json
. Interestingly, if I don't use the "+" and change it to a "-", It will match. Something may be going on with matching "+" signs? A similar contentType is used in my company that I cannot change. Do you have any ideas on this? @Matt (pactflow.io / pact-js / pact-go)
here's an updated test:
Copy code
const { pactWith } = require("jest-pact/v3");
const { MatchersV3 } = require("@pact-foundation/pact/v3");
const mockPactServerClient = require("../../utility/mockPactServer");

const { string } = MatchersV3;

pactWith(
  {
    consumer: "bulk-upload",
    provider: "upload-provider",
    logLevel: "INFO",
    port: 8081,
  },
  (interaction) => {
    interaction("initiateUpload", ({ provider, execute }) => {
        beforeEach(() =>
            provider
                .given("An experiance user")
                //These can share a state handler. However because we don't actually need to do anything, it's not needed in this case.
                .uponReceiving("A disallowed but allowed asset name")
                .withRequest({
                    method: "POST",
                    path: "/bar/foo",
                    headers: {
                        Authorization: string("headerbar"),
                    },
                    body: {
                        "dc:format": "image/jpeg",
                    },
                    contentType: 'application/foo.bar.bulk-transfer+json',
                })
                .willRespondWith({
                    status: 200,
                    headers: {},
                    body: {
                        "dc:format": string("image/jpeg"),
                    },
                    contentType: 'application/foo.bar.bulk-transfer+json',
                })
        );
        execute("Create Disallowed Asset", () => {
            return <http://mockPactServerClient.post|mockPactServerClient.post>(
                "/bar/foo",
                {
                    "dc:format": "image/jpeg",
                },
                {
                    headers: {
                        Authorization: "Bearer Auth",
                        "Content-Type": 'application/foo.bar.bulk-transfer+json',
                    },
                }
            );
        });
    });
  }
);
if anyone has a similar issue as I've described above, I got it to work using regex
remove the contentType property, and throw it back into the header.
Copy code
"Content-Type": regex(
/application\/vfoo.bar.bulk-transfer\+json/,                  "application/foo.bar.bulk-transfer+json"),
This is what the regex will look like, and it will match on the incoming header from a request
Note: the regex only needs to be put in the .withRequest,
m
hm interesting
, if I don’t use the “+” and change it to a “-”, It will match. Something may be going on with matching “+” signs?
smells of escaping or something doesn’t it.
If you could please set the log level to debug and provide an output dump, that would be helpful in seeing what the engine thinks is goinsg on