无法确保在开玩笑的消费者契约请求中传递基数 URL
Trouble ensuring base URL is passed in jest consumer pact requests
我正在编写一个服务消费者契约测试,似乎在进行 API 调用时,它会从请求路径中删除基础 url。
对于上下文,这是我正在尝试的测试 运行。
import { pactWith } from 'jest-pact';
import { Matchers } from '@pact-foundation/pact';
import { ProviderApi } from 'provider-app-api';
import fetch from 'node-fetch';
globalThis.fetch = fetch;
pactWith(
{ consumer: 'ConsumerApp', provider: 'ProviderApp', port: 1234 },
(provider) => {
let providerApi;
beforeEach(() => {
providerApi = new ProviderApi(
provider.mockService.baseUrl,
'access_token'
);
});
describe('ProviderApp API', () => {
beforeEach(() => {
return provider.addInteraction({
state: 'A get request to /segments/{segment_code}/makes',
uponReceiving: 'Some makes exist with segment code vehicles',
withRequest: {
method: 'GET',
path: `${provider.mockService.baseUrl}/segments/vehicles/makes`,
headers: { Authorization: 'Bearer access_token' },
},
willRespondWith: {
status: 200,
headers: { 'Content-Type': 'application/json; charset=utf8' },
body: Matchers.like({
id: 1,
code: 'TOYO',
description: 'Toyota',
start_year: 2011,
end_year: 2021,
segment_code: 'vehicles',
}),
},
});
});
it('returns a successful body', () => {
return vehiclelinkApi.fetchMakes('vehicles').then((response) => {
// assertions to go here
expect(true).toBeTruthy();
});
});
});
}
);
在 运行 测试后,我得到了这个输出:
$ yarn run test:consumer_pact
console.error
at node_modules/@pact-foundation/src/httpPact.ts:121:17
console.error
Pact verification failed!
at node_modules/@pact-foundation/src/httpPact.ts:122:17
console.error
Actual interactions do not match expected interactions for mock MockService.
Missing requests:
GET http://127.0.0.1:1234/segments/vehicles/makes
Unexpected requests:
GET /segments/vehicles/makes
See /home/stefan/project/pact/logs/ConsumerApp-ProviderApp-mockserver-interaction-port-1234.log for details.
它似乎正在从 fetch 调用中删除基础 URL,因此 pact 服务器永远不会收到请求,这是有道理的。当我使用 ProviderAPI 时,如何强制将其附加到调用中?我已确保我在请求中传递了 provider.mockService.baseUrl
,并且我已确保该值为 localhost:1234
。这是一个需要在 ProviderApi 包内解决的问题吗?
那个 baseUrl 不应该在 path
属性 中,它应该只采用路径,而不是完整的 URI
即应该就是这个
path: "/segments/vehicles/makes",
我正在编写一个服务消费者契约测试,似乎在进行 API 调用时,它会从请求路径中删除基础 url。
对于上下文,这是我正在尝试的测试 运行。
import { pactWith } from 'jest-pact';
import { Matchers } from '@pact-foundation/pact';
import { ProviderApi } from 'provider-app-api';
import fetch from 'node-fetch';
globalThis.fetch = fetch;
pactWith(
{ consumer: 'ConsumerApp', provider: 'ProviderApp', port: 1234 },
(provider) => {
let providerApi;
beforeEach(() => {
providerApi = new ProviderApi(
provider.mockService.baseUrl,
'access_token'
);
});
describe('ProviderApp API', () => {
beforeEach(() => {
return provider.addInteraction({
state: 'A get request to /segments/{segment_code}/makes',
uponReceiving: 'Some makes exist with segment code vehicles',
withRequest: {
method: 'GET',
path: `${provider.mockService.baseUrl}/segments/vehicles/makes`,
headers: { Authorization: 'Bearer access_token' },
},
willRespondWith: {
status: 200,
headers: { 'Content-Type': 'application/json; charset=utf8' },
body: Matchers.like({
id: 1,
code: 'TOYO',
description: 'Toyota',
start_year: 2011,
end_year: 2021,
segment_code: 'vehicles',
}),
},
});
});
it('returns a successful body', () => {
return vehiclelinkApi.fetchMakes('vehicles').then((response) => {
// assertions to go here
expect(true).toBeTruthy();
});
});
});
}
);
在 运行 测试后,我得到了这个输出:
$ yarn run test:consumer_pact
console.error
at node_modules/@pact-foundation/src/httpPact.ts:121:17
console.error
Pact verification failed!
at node_modules/@pact-foundation/src/httpPact.ts:122:17
console.error
Actual interactions do not match expected interactions for mock MockService.
Missing requests:
GET http://127.0.0.1:1234/segments/vehicles/makes
Unexpected requests:
GET /segments/vehicles/makes
See /home/stefan/project/pact/logs/ConsumerApp-ProviderApp-mockserver-interaction-port-1234.log for details.
它似乎正在从 fetch 调用中删除基础 URL,因此 pact 服务器永远不会收到请求,这是有道理的。当我使用 ProviderAPI 时,如何强制将其附加到调用中?我已确保我在请求中传递了 provider.mockService.baseUrl
,并且我已确保该值为 localhost:1234
。这是一个需要在 ProviderApi 包内解决的问题吗?
那个 baseUrl 不应该在 path
属性 中,它应该只采用路径,而不是完整的 URI
即应该就是这个
path: "/segments/vehicles/makes",