pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.63k stars 348 forks source link

Error ocurred in mock service: JSON::ParserError - 757: unexpected token at '"JSON"' #783

Closed krishnaraghavendra closed 2 years ago

krishnaraghavendra commented 2 years ago

Expected behaviour

Make a request successfully

Actual behaviour

Error ocurred in mock service: JSON::ParserError - 757: unexpected token at '"[{\"test1\":\"test-123\",\"test4\":[\"ns1.test\",\"ns2.test\"],\"test2\":\"10.0.0.1\",\"test3\":\"10.0.0.1/27\"}]"'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/json-2.5.1/lib/json/common.rb:216:in `parse'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/json-2.5.1/lib/json/common.rb:216:in `parse'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/consumer/mock_service/rack_request_helper.rb:33:in `request_as_hash_from'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/mock_service/request_handlers/interaction_replay.rb:45:in `respond'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/mock_service/request_handlers/base_request_handler.rb:17:in `call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/rack-2.1.4/lib/rack/cascade.rb:35:in `block in call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/rack-2.1.4/lib/rack/cascade.rb:26:in `each'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/rack-2.1.4/lib/rack/cascade.rb:26:in `call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/consumer/mock_service/cors_origin_header_middleware.rb:11:in `call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/consumer/mock_service/error_handler.rb:13:in `call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/mock_service/app.rb:34:in `call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/pact-mock_service-3.9.0/lib/pact/consumer/mock_service/set_location.rb:14:in `call'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/rack-2.1.4/lib/rack/handler/webrick.rb:88:in `service'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/webrick-1.3.1/lib/webrick/httpserver.rb:138:in `service'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/webrick-1.3.1/lib/webrick/httpserver.rb:94:in `run'
node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.81/pact/lib/vendor/ruby/2.2.0/gems/webrick-1.3.1/lib/webrick/server.rb:191:in `block in start_thread'

Steps to reproduce

I am using jest pact version - 0.9.1, when trying to make a post request , i get the above error. I am making post request using open api generated sdk. My post requests accepts an array of objects as input

mefellows commented 2 years ago

This is not a very helpful bug report in the current form. Can you please create a minimal example we can use to reproduce the problem?

For example, without seeing your test setup and the actual call being made we don't have much to go on.

krishnaraghavendra commented 2 years ago

`let client: BaseAPI; beforeAll(() => { const conf = new Configuration({ basePath: mockProvider.mockService.baseUrl, });

  client = new BaseAPI(conf);
});

describe('create test', () => {
  it('with sucessfull response', async () => {
      interaction:{
          state: test,
          uponReceiving: test,
          withRequest: {
            method: POST,
            path: /test,
            body:[
              {
                test1: test1-123,
                test2:[
                  ns1.test2,
                  ns2.test2
                ],
                test3: 10.0.0.1,
                test4: 10.0.0.1/27
              }
            ]
          },
          willRespondWith: {
            status: 201,
            headers: {
              Content-Type: application/json; charset=utf-8
            },
            body: {
              test: 1,
              teststatus: ACCEPTED
            }
          }
        }
    await mockProvider.addInteraction(interaction);

    // eslint-disable-next-line max-len
    const { test } = (await client.create(1, interaction.withRequest.body)).data;

    console.log(test);

  });`
mefellows commented 2 years ago

I think your code snippet is broken. For example, the body section has unescaped property values which doesn't appear to be valid JSON or JS - did you write this by hand? If not, this would be worth looking into first.

Also, you removed the section of the bug report template that requests log information. I'd like both the full terminal output at DEBUG level and the pact log file (see https://github.com/pact-foundation/pact-js#troubleshooting--faqs for how to do this). This will help us investigate where it is failing.

Lastly, this is still not reproducable. Depending on the above and where the failure is in the lifecycle of things (hopefully it's at mockProvider.addInteraction(...)), we will likely need to know what client.create(...) actually does.

krishnaraghavendra commented 2 years ago

Interaction is

export const interaction = {
              state: 'test',
              uponReceiving: 'test',
              withRequest: {
                method: HTTPMethod.POST,
                path: '/test',
                body:[
                  {
                    test1: 'test1-123',
                    test2:[
                      'ns1.test2',
                      'ns2.test2'
                    ],
                    test3: '10.0.0.1',
                    test4: '10.0.0.1/27'
                  }
                ]
              },
              willRespondWith: {
                status: 201,
                headers: {
                  Content-Type: 'application/json; charset=utf-8'
                },
                body: {
                  test: '1',
                  teststatus: 'ACCEPTED'
                }
              }
}

consumer test is

pactWith({
  consumer: 'test-sdk',
  provider: 'test',
  log: process.env.output_dir
    ? resolve(process.env.output_dir, 'consumer-pact-logs') : 'outputs/consumer-pact-logs',
  dir: process.env.output_dir ? resolve(process.env.output_dir, 'pacts') : 'outputs/pacts',
}, (mockProvider) => {
  describe(`Consumer tests for ${pkgJson.name} using latest ${pkgJson.name}-sdk`, () => {
    let client: BaseAPI;
    beforeAll(() => {
      const conf = new Configuration({
        basePath: mockProvider.mockService.baseUrl,
      });

      client = new BaseAPI(conf);
    });

    describe('create test', () => {
      it('with sucessfull response', async () => {

        await mockProvider.addInteraction(interaction);

        // eslint-disable-next-line max-len
        const { test } = (await client.create(1, interaction.withRequest.body)).data;

        console.log(test);

      });
    });
  });
});
mefellows commented 2 years ago

Thanks, this helps a little. For future reference,you should use the triple backtick (or use the code block button) from the editor to properly format your code. i've done that now.

I still need your logs, and I still need to know exactly what your API client is doing.

bethesque commented 2 years ago

It looks like the body of the request has double quotes around it.