call real web service code in order to create pact files

61 views Asked by At

can you please help me on below scenario?

i am doing poc on pact and need help on understanding the concept . do we need to call real web service call code in order to create pact files? if yes, how to create pact files for existing API code

def test_HappyPath (self):
           mockurl = 'http://localhost:1234'
           expected =  {body:true}
           pact.given (
               'Given there is a valid  form'
           ).upon_receiving (
               'fetch all the info '
           ).with_request (
                'get',
               '/',headers={Authorization:'Bearer 58771381-333e-334f-9604-784'}
           ).will_respond_with(200, body=expected)
 with pact:
               result = callAPI ( mockurl )
           self.assertEqual(result, expected )

def callAPI (url):
    return requests.get ( url ).json()

my understanding is callAPI is real production web service call ? am i correct ? if yes, do you have any sample code to return requested response from real service. i am kind of stuck in this specific part to complete my POC on pact

Thank you in Advance

1

There are 1 answers

0
Matthew Fellows On

callAPI should be the code in your consumer that is normally responsible for calling a remote API (provider). As part of your test setup, you should configure your system to point at the local mock server that Pact starts in place of the real server.

When callAPI is executed, it will get back a 200 with your expected body. You should then perform some checks that callAPI worked as expected.