phiremock icon indicating copy to clipboard operation
phiremock copied to clipboard

How to mock a response from a third party api

Open dacog opened this issue 3 years ago • 3 comments

Hi,

I am looking for a tool to do snapshot testing for third-party APIs, for example https://graph.facebook.com or Google Ad API.

I managed to install and configure Phiremock in a project and added an expectation, which I can get by using Insomnia as a client.

Now, I was trying to set up Phiremox with the Codeception module using the same expectation, but I failed to get it to run.

My use-case is as follows:

  1. I have a class which makes several API calls to, for example, graph.facebook.com through Facebook Business SDK Library.
  2. I want to use snapshot testing to record (or prepare) the responses from the API so that I can use them in acceptance tests without calling the external API.
  3. In the acceptance test, I initialize a Phiremock client and set it to connect to my Phiremock server.
  4. I run the method which calls the external API and I expect Phiremock to somehow hijack that call and give the expectation as response.

Am I right to think that Phiremock can do this? If so, can you point me in the right direction to achieve this?

I guess point 4 is my problem, as I hope that Phiremock magically hijacks the call. Regrettably, I have tried to find out how to make this work, but I haven't been able to.

I have tried using

url": {
      "isEqualTo": "https://graph.facebook.com/v10.0/act_866830094136812/campaigns"
    },

to set the URL in the expectation

If I set it to

url": {
      "isEqualTo": "/act_866830094136812/campaigns"
    },

I can get the expectation with a call to localhost:8086/act_866830094136812/campaignsfrom Insomnia.

In the Test I initialize a client by using

$phiremockClient = PhiremockClientFactory::createDefault()->createPhiremockClient(
            new Host('localhost'),
            new Port('8086')
        );

but then, after reading all I could find, I don't know how to move forward.

I will appreciate any pointers and any help :)

Best,

Diego

dacog avatar Jul 29 '21 14:07 dacog

Hello @dacog. Thank you for trying out Phiremock :)

The Phiremock server won't intercept requests automatically, you need to configure your application so while it's being tested it sends the requests to it. I already faced the issue of testing APIs with a client/sdk provided by a third party and they usually have the base url hardcoded, which makes it difficult to test. My solution usually is to wrap the client on my own class where I can provide the original URL when on production or staging environments and phiremock's for the testing environment. Hope this helps.

Best regards, Mariano.

mcustiel avatar Jul 30 '21 13:07 mcustiel

Here you can see how the application should be configured so phiremock is requested: https://github.com/mcustiel/phiremock-server#how-does-it-work

mcustiel avatar Jul 30 '21 13:07 mcustiel

Hi @mcustiel

Thank you very much for the clarification. Now I understand what that part meant :)

I also understand now my problem. I thought that Phiremock also worked as a proxy and could intercept the calls.

Have you ever tried adding the domains to the /etc/hosts file to redirect all calls to a certain domain to localhost or to use dnsmasq to make the redirect? This may solve the interception of the call without creating a new class.

Best regards,

Diego

dacog avatar Jul 30 '21 14:07 dacog