Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get API mocking non-default responses to work correctly on Windows #147

Closed
chrisprijic opened this issue May 18, 2018 · 5 comments
Closed

Comments

@chrisprijic
Copy link

chrisprijic commented May 18, 2018

Overview

Checking in from sendgrid/sendgrid-go#257 with some issues using a beforeScript to enable mocking.

Their implementation includes an option to provide an X-Mock header to the API being mocked, and using a similar beforeScript to enable mocking and choosing the response to return from it:

function (ctx, request) {
    var mock = request.header.get("X-Mock");
    if (mock) {
        ctx.mock.set(true, mock);
    }
}

Where the value of the X-Mock header is the status code for the response -- a la 400 or 413 or 200.

However, when using their mock API, I always get the standard response, and the X-Mock header is ignored. I even tried adding additional logging in the beforeScript but could not get anything to work.

I'm hoping that posting here can help me find some insights into getting this working!


Details (from sendgrid/sendgrid-go#257)

OS: Windows 10
Using: prism.exe (x64)
Version: 2.0.9
command: prism.exe mock -d -s https://raw.githubusercontent.com/sendgrid/sendgrid-oai/master/oai_stoplight.json -p 4000

The above utilizes an X-Mock header to specify the response code to mock back. However, even when mocking endpoints like POST /alerts (below) without a request body, and with X-Mock header set to 400, I still get a 201 response code and a 201 response body.

example cURL:

curl --header "X-Mock: 400" --header "Authorization: Bearer SENDGRID_APIKEY" http://localhost:4000/v3/alerts -X POST

example output:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 4000 (#0)
> POST /v3/alerts HTTP/1.1
> Host: localhost:4000
> User-Agent: curl/7.55.1
> Accept: */*
> X-Mock: 400
> Authorization: Bearer SENDGRID_APIKEY
>
< HTTP/1.1 201 200 OK
< Content-Length: 135
< Content-Type: application/json
< Sl-Response-Valid: true
<
{"created_at":1451520930,"email_to":"[email protected]","frequency":"daily","id":48,"type":"stats_notification","updated_at":1451520930}* Connection #0 to host localhost left intact

When running that cURL command, it should return the provided 400 response, but instead it still returns a 201.


Question

Is there a better way to do this, or is it clear what I'm doing wrong? I need to get this working in order to test API Client methods I'm writing for the API.

@pytlesk4
Copy link
Contributor

@chrisprijic great question! So you are using a completely different version of prism, and the way mocking works with the above command is a little bit different. You don't need any javascript actually, and to control the status code, use the query string parameter __code=400 to change the code being mocked back.

Let me know if you have any issues with that, tomorrow I will create a mock server for you that uses a header. Are you trying to do something similar to sendgrid with your sdks?

@chrisprijic
Copy link
Author

@pytlesk4 thanks! That worked great, and as expected.

Is there any documentation, or a point of origin this is from, so I can let SendGrid know (I'll obviously ref this issue)?

@pytlesk4
Copy link
Contributor

So there are some docs on prism here, https://docs.stoplight.io.

And then for the mock server, and how it works, there are some docs in this scenario description. I actually need to update that description because it has changed quite a bit, but those options still work. I will do that tomorrow when I get into work.

@pytlesk4
Copy link
Contributor

@chrisprijic also this blog post talks about one of the new features.

@chrisprijic
Copy link
Author

@pytlesk4 thanks for all this, and the rapid responses. I'm new to prism and am using through another repo (see refs), so this is good for me to know.

I'm good to go now, and I'll work on updating my tests to use the querystring parameter rather than the beforeScript I was doing before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants