1+ "use strict"
2+
3+ const expect = require ( "chai" ) . expect ;
4+ const path = require ( "path" ) ;
5+ const { Pact } = require ( '@pact-foundation/pact' ) ;
6+ const { helloworldv2 } = require ( "../index" ) ;
7+
8+ describe ( "The Helloworld API" , ( ) => {
9+ let url = "http://localhost"
10+ const port = 8992
11+ const contractPath = path . resolve ( process . cwd ( ) , "pacts" ) ;
12+
13+ const provider = new Pact ( {
14+ port : port ,
15+ log : path . resolve ( process . cwd ( ) , "logs" , "mockserver-integration.log" ) ,
16+ dir : contractPath ,
17+ spec : 2 ,
18+ consumer : "consumer-js" ,
19+ provider : "HelloWorld" ,
20+ pactfileWriteMode : "merge" ,
21+ publishPacts : true ,
22+ publishVerificationResult : true
23+ } )
24+
25+ const EXPECTED_BODY = {
26+ message : "Hello world v2"
27+ }
28+
29+ // Setup the provider
30+ before ( ( ) => provider . setup ( ) )
31+
32+ // Write Pact when all tests done
33+ after ( ( ) => provider . finalize ( ) )
34+
35+ // verify with Pact, and reset expectations
36+ afterEach ( ( ) => provider . verify ( ) )
37+
38+ describe ( "get /api/v2.0/helloworld" , ( ) => {
39+ before ( done => {
40+ const interaction = {
41+ state : "consumer-js get at helloworld" ,
42+ uponReceiving : "A message" ,
43+ withRequest : {
44+ method : "GET" ,
45+ path : "/api/v2.0/helloworld"
46+ } ,
47+ willRespondWith : {
48+ status : 200 ,
49+ headers : {
50+ "Content-Type" : "application/json; charset=utf-8" ,
51+ } ,
52+ body : EXPECTED_BODY ,
53+ } ,
54+ }
55+ provider . addInteraction ( interaction ) . then ( ( ) => {
56+ done ( )
57+ } )
58+ } )
59+
60+ it ( "returns the correct response" , done => {
61+ const urlAndPort = {
62+ url : url ,
63+ port : port ,
64+ }
65+ helloworldv2 ( urlAndPort ) . then ( response => {
66+ expect ( response . data ) . to . eql ( EXPECTED_BODY )
67+ publish ( contractPath )
68+
69+ done ( )
70+ } , done
71+ )
72+ } )
73+ } )
74+ } )
75+
76+ let publish = contractDirectory => {
77+ let pact = require ( '@pact-foundation/pact-node' ) ;
78+ let opts = {
79+ pactBroker : "http://localhost/" ,
80+ pactFilesOrDirs : [ contractDirectory ] ,
81+ consumerVersion : "0.0.1" ,
82+ publishVerificationResult : true
83+ } ;
84+ pact . publishPacts ( opts ) ;
85+ }
0 commit comments