Javascript PACT.io:获取丢失的请求错误和错误:跨源http://localhost 被禁止的
我正在使用Pact.io在我的消费者中生成契约测试,但我得到了错误:Javascript PACT.io:获取丢失的请求错误和错误:跨源http://localhost 被禁止的,javascript,testing,axios,jestjs,pact,Javascript,Testing,Axios,Jestjs,Pact,我正在使用Pact.io在我的消费者中生成契约测试,但我得到了错误: 缺少请求:获取http://localhost:3001/productId/2857?date=2021-05-31 我将pact实例配置为在端口3001上运行,但我认为请求没有通过端口3001,因为我遇到了另一个错误: 错误:跨原点http://localhost Dispatch时禁止出错 我已经尝试使用axios.defaults.adapter=require('axios/lib/adapters/http') 但
缺少请求:获取http://localhost:3001/productId/2857?date=2021-05-31
我将pact实例配置为在端口3001上运行,但我认为请求没有通过端口3001,因为我遇到了另一个错误:
错误:跨原点http://localhost Dispatch时禁止出错
我已经尝试使用axios.defaults.adapter=require('axios/lib/adapters/http')代码>
但我还是有交叉来源http://localhost 在调度错误时禁止
已尝试使用jest--env=node
和“TestenEnvironment”:“node”
但此选项破坏了我的代码:
ReferenceError: self is not defined
> 1 | import { request } from './requests';
有人能帮我吗
我的代码是:
package.json代码:
...
"scripts": {
...
"test:consumer": "jest app/tests/contract/consumer/*.test.js --runInBand --setupFiles ./app/tests/helpers/pactSetup.js --setupTestFrameworkScriptFile=./app/tests/helpers/pactTestWrapper.js",
...
}
...
ContractTest\u客户端Consumer.test.js代码:
import axios from 'axios';
import { Matchers } from '@pact-foundation/pact';
import { provider } from '../../helpers/pactSetup';
import viewApi from './viewApi';
import config from '../../../../app/config';
const getApiEndpoint = 'http://localhost:3001';
axios.defaults.adapter = require('axios/lib/adapters/http');
const productDateResponse = { value: 100, day: "2021-05-31" }
describe('Product Date', () => {
afterEach(() => provider.verify());
describe('Get Product Value', () => {
beforeEach(() => {
const interaction = {
state: 'check some product value in some day',
uponReceiving: 'value product in some day',
withRequest: {
method: 'GET',
path: `${config.API_URL}/productId/2857?date=2021-05-31`,
headers: {
Accept: 'application/json, text/plain, */*',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: Matchers.somethingLike(productDateResponse),
},
};
return provider.addInteraction(interaction);
});
test('returns correct body, header and statusCode', () => {
console.log('Before calling getProduct');
const response = viewApi.getProduct(2857, '2021-05-31', null);
console.log('Called getProduct');
console.log(response);
console.log('After print response');
expect(response.headers['content-type']).toBe('application/json; charset=utf-8');
expect(response.data).toEqual(productDateResponse);
expect(response.status).toEqual(200);
});
});
});
import { request } from './requests';
import config from 'config';
import { response } from 'express';
export default class ViewApi {
static getProduct(productId, date, requestDate = null) {
try {
const url = `${config.API_URL}/productId/${productId}`;
let queryParams = `date=${date}`;
if (requestDate) {
queryParams += `&requestDate=${requestDate}`;
}
return request(`${url}?${queryParams}`);
}
}
import path from 'path';
import { Pact } from '@pact-foundation/pact';
export const provider = new Pact({
port: 3001,
log: path.resolve(process.cwd(), 'app/tests/contract/logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'app/tests/contract/pacts'),
spec: 2,
logLevel: 'DEBUG',
pactfileWriteMode: 'overwrite',
consumer: 'pwa-store',
provider: 'api-store',
});
import { provider } from './pactSetup';
beforeAll(() => provider.setup());
afterAll(() => provider.finalize());
viewApi.js代码:
import axios from 'axios';
import { Matchers } from '@pact-foundation/pact';
import { provider } from '../../helpers/pactSetup';
import viewApi from './viewApi';
import config from '../../../../app/config';
const getApiEndpoint = 'http://localhost:3001';
axios.defaults.adapter = require('axios/lib/adapters/http');
const productDateResponse = { value: 100, day: "2021-05-31" }
describe('Product Date', () => {
afterEach(() => provider.verify());
describe('Get Product Value', () => {
beforeEach(() => {
const interaction = {
state: 'check some product value in some day',
uponReceiving: 'value product in some day',
withRequest: {
method: 'GET',
path: `${config.API_URL}/productId/2857?date=2021-05-31`,
headers: {
Accept: 'application/json, text/plain, */*',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: Matchers.somethingLike(productDateResponse),
},
};
return provider.addInteraction(interaction);
});
test('returns correct body, header and statusCode', () => {
console.log('Before calling getProduct');
const response = viewApi.getProduct(2857, '2021-05-31', null);
console.log('Called getProduct');
console.log(response);
console.log('After print response');
expect(response.headers['content-type']).toBe('application/json; charset=utf-8');
expect(response.data).toEqual(productDateResponse);
expect(response.status).toEqual(200);
});
});
});
import { request } from './requests';
import config from 'config';
import { response } from 'express';
export default class ViewApi {
static getProduct(productId, date, requestDate = null) {
try {
const url = `${config.API_URL}/productId/${productId}`;
let queryParams = `date=${date}`;
if (requestDate) {
queryParams += `&requestDate=${requestDate}`;
}
return request(`${url}?${queryParams}`);
}
}
import path from 'path';
import { Pact } from '@pact-foundation/pact';
export const provider = new Pact({
port: 3001,
log: path.resolve(process.cwd(), 'app/tests/contract/logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'app/tests/contract/pacts'),
spec: 2,
logLevel: 'DEBUG',
pactfileWriteMode: 'overwrite',
consumer: 'pwa-store',
provider: 'api-store',
});
import { provider } from './pactSetup';
beforeAll(() => provider.setup());
afterAll(() => provider.finalize());
pactsetup.js代码:
import axios from 'axios';
import { Matchers } from '@pact-foundation/pact';
import { provider } from '../../helpers/pactSetup';
import viewApi from './viewApi';
import config from '../../../../app/config';
const getApiEndpoint = 'http://localhost:3001';
axios.defaults.adapter = require('axios/lib/adapters/http');
const productDateResponse = { value: 100, day: "2021-05-31" }
describe('Product Date', () => {
afterEach(() => provider.verify());
describe('Get Product Value', () => {
beforeEach(() => {
const interaction = {
state: 'check some product value in some day',
uponReceiving: 'value product in some day',
withRequest: {
method: 'GET',
path: `${config.API_URL}/productId/2857?date=2021-05-31`,
headers: {
Accept: 'application/json, text/plain, */*',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: Matchers.somethingLike(productDateResponse),
},
};
return provider.addInteraction(interaction);
});
test('returns correct body, header and statusCode', () => {
console.log('Before calling getProduct');
const response = viewApi.getProduct(2857, '2021-05-31', null);
console.log('Called getProduct');
console.log(response);
console.log('After print response');
expect(response.headers['content-type']).toBe('application/json; charset=utf-8');
expect(response.data).toEqual(productDateResponse);
expect(response.status).toEqual(200);
});
});
});
import { request } from './requests';
import config from 'config';
import { response } from 'express';
export default class ViewApi {
static getProduct(productId, date, requestDate = null) {
try {
const url = `${config.API_URL}/productId/${productId}`;
let queryParams = `date=${date}`;
if (requestDate) {
queryParams += `&requestDate=${requestDate}`;
}
return request(`${url}?${queryParams}`);
}
}
import path from 'path';
import { Pact } from '@pact-foundation/pact';
export const provider = new Pact({
port: 3001,
log: path.resolve(process.cwd(), 'app/tests/contract/logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'app/tests/contract/pacts'),
spec: 2,
logLevel: 'DEBUG',
pactfileWriteMode: 'overwrite',
consumer: 'pwa-store',
provider: 'api-store',
});
import { provider } from './pactSetup';
beforeAll(() => provider.setup());
afterAll(() => provider.finalize());
pacttetwrapper.js代码:
import axios from 'axios';
import { Matchers } from '@pact-foundation/pact';
import { provider } from '../../helpers/pactSetup';
import viewApi from './viewApi';
import config from '../../../../app/config';
const getApiEndpoint = 'http://localhost:3001';
axios.defaults.adapter = require('axios/lib/adapters/http');
const productDateResponse = { value: 100, day: "2021-05-31" }
describe('Product Date', () => {
afterEach(() => provider.verify());
describe('Get Product Value', () => {
beforeEach(() => {
const interaction = {
state: 'check some product value in some day',
uponReceiving: 'value product in some day',
withRequest: {
method: 'GET',
path: `${config.API_URL}/productId/2857?date=2021-05-31`,
headers: {
Accept: 'application/json, text/plain, */*',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: Matchers.somethingLike(productDateResponse),
},
};
return provider.addInteraction(interaction);
});
test('returns correct body, header and statusCode', () => {
console.log('Before calling getProduct');
const response = viewApi.getProduct(2857, '2021-05-31', null);
console.log('Called getProduct');
console.log(response);
console.log('After print response');
expect(response.headers['content-type']).toBe('application/json; charset=utf-8');
expect(response.data).toEqual(productDateResponse);
expect(response.status).toEqual(200);
});
});
});
import { request } from './requests';
import config from 'config';
import { response } from 'express';
export default class ViewApi {
static getProduct(productId, date, requestDate = null) {
try {
const url = `${config.API_URL}/productId/${productId}`;
let queryParams = `date=${date}`;
if (requestDate) {
queryParams += `&requestDate=${requestDate}`;
}
return request(`${url}?${queryParams}`);
}
}
import path from 'path';
import { Pact } from '@pact-foundation/pact';
export const provider = new Pact({
port: 3001,
log: path.resolve(process.cwd(), 'app/tests/contract/logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'app/tests/contract/pacts'),
spec: 2,
logLevel: 'DEBUG',
pactfileWriteMode: 'overwrite',
consumer: 'pwa-store',
provider: 'api-store',
});
import { provider } from './pactSetup';
beforeAll(() => provider.setup());
afterAll(() => provider.finalize());
我有一个名为API_URL的环境变量,我用它来定义:
导出API\u URL=http://localhost:3001
我认为以下内容不会影响您的请求
对象(我假设它是axios客户端?),因为它发生在加载模块之后,并且只适用于默认客户端
axios.defaults.adapter = require('axios/lib/adapters/http');
您可能只需将适配器直接应用于请求
客户端,但值得查看Axios文档
我认为这些是你的选择:
修复您的/request
客户端,以便使用更新的适配器对其进行配置
在pact对象上设置cors:true
,这样它就可以像通过web应用发送一样响应选项
标题(似乎是默认模式)
(2) 如果它更符合您的实际使用情况,可能更有意义