added pact-node-provider example

This commit is contained in:
Tom Hombergs
2018-10-28 04:17:28 +01:00
parent 5fb2d69ca7
commit 65c49a3ea7
9 changed files with 2441 additions and 29 deletions

View File

@@ -10,7 +10,7 @@ global.provider = new Pact({
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
pactfileWriteMode: 'update',
consumer: 'react-consumer',
provider: 'node-provider',
consumer: 'hero-consumer',
provider: 'hero-provider',
host: '127.0.0.1'
});

View File

@@ -1,6 +1,7 @@
import Hero from "./hero";
const axios = require('axios');
import adapter from 'axios/lib/adapters/http';
class HeroService {
@@ -18,9 +19,9 @@ class HeroService {
url: `/heroes/${heroId}`,
baseURL: `${this.baseUrl}:${this.port}`,
headers: {
'Accept': 'application/json'
'Accept': 'application/json; charset=utf-8'
}
}).then((response) => {
}, adapter).then((response) => {
const hero = response.data;
return new Promise((resolve, reject) => {
try {
@@ -40,11 +41,11 @@ class HeroService {
url: `/heroes`,
baseURL: `${this.baseUrl}:${this.port}`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'Accept': 'application/json; charset=utf-8',
'Content-Type': 'application/json; charset=utf-8'
},
data: hero
}).then((response) => {
}, adapter).then((response) => {
const hero = response.data;
return new Promise((resolve, reject) => {
try {

View File

@@ -6,6 +6,13 @@ describe('HeroService API', () => {
const heroService = new HeroService('http://localhost', global.port);
// a matcher for the content type "application/json" in UTF8 charset
// that ignores the spaces between the ";2 and "charset"
const contentTypeJsonMatcher = Pact.Matchers.term({
matcher: "application\\/json; *charset=utf-8",
generate: "application/json; charset=utf-8"
});
describe('getHero()', () => {
beforeEach((done) => {
@@ -16,13 +23,13 @@ describe('HeroService API', () => {
method: 'GET',
path: '/heroes/42',
headers: {
'Accept': 'application/json'
'Accept': contentTypeJsonMatcher
}
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json'
'Content-Type': contentTypeJsonMatcher
},
body: Pact.Matchers.somethingLike(new Hero('Superman', 'flying', 'DC', 42))
}
@@ -54,15 +61,18 @@ describe('HeroService API', () => {
method: 'POST',
path: '/heroes',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'Accept': contentTypeJsonMatcher,
'Content-Type': contentTypeJsonMatcher
},
body: new Hero('Superman', 'flying', 'DC')
},
willRespondWith: {
status: 201,
headers: {
'Content-Type': 'application/json'
'Content-Type': Pact.Matchers.term({
matcher: "application\\/json; *charset=utf-8",
generate: "application/json; charset=utf-8"
})
},
body: Pact.Matchers.somethingLike(
new Hero('Superman', 'flying', 'DC', 42))