Behat Acceptance Test Identify Response Type - behat

I am writing some tests for my application that is expected to return Json response and i wish to ensure it by using Behat.
Scenario: Response is json
Given I am on the domain "api.mydomain.net"
When I call the webservice "api" with GET
Then the webservice response content type is "application/json"
I researched at documentation of Behat but couldn't find any useful resource to assert response type.
Does anyone experienced the same situation and came up with a solution?

Related

Karate send json body with GET Method [duplicate]

This question already has an answer here:
Karate API : How to hit an endpoint url with post method which does not have request body
(1 answer)
Closed 1 year ago.
How to provide request body for GET method using Karate API.
When trying to provide Request body for Get method in Karate API, it doesnt take the request and throws 500 status code.
As stated in HTTP GET with request body
using the request body within a GET request to change the semantics of the request is highly discouraged.
It's debatable that Karate ignores the message body when using method GET and if you think there is a use case for that feature your free to file an issue.
Furthermore, the 500 status code you observe is returned by your system under test because of the missing message body and has nothing to do with karate.
EDIT: confirmed that Karate 1.0 will support a body along with a GET

How to provide JSON request body for GET method in Karate API [duplicate]

This question already has an answer here:
Karate API : How to hit an endpoint url with post method which does not have request body
(1 answer)
Closed 1 year ago.
How to provide request body for GET method using Karate API.
When trying to provide Request body for Get method in Karate API, it doesnt take the request and throws 500 status code.
As stated in HTTP GET with request body
using the request body within a GET request to change the semantics of the request is highly discouraged.
It's debatable that Karate ignores the message body when using method GET and if you think there is a use case for that feature your free to file an issue.
Furthermore, the 500 status code you observe is returned by your system under test because of the missing message body and has nothing to do with karate.
EDIT: confirmed that Karate 1.0 will support a body along with a GET

How to assert grpc method response body

I'm verifying response of a grpc service method.
Right now im asserting response by taking out every parameter and verifying it against my expected test parameter.If there are many parameters to assert, it becomes a tedious process.
So im looking for something like 'Json comparator' which will verify my grpc response message with expected message .
I'm also thinking of converting my expected and my actual messages to json and verifying it using json comparator.But this is not straightforward.
So any other thoughts?
Protobuf messages override equals(), so you can hard-code the expected response message and use normal assertEquals(expected, actual) in JUnit.

How to use a Postman Mock Server

I have followed the guide here to create a postman mock for a postman collection. The mock seem to be successfully created, but I have no idea how to use the mock service.
I've been given a url for the mock, but how do I specify one of my requests? If I issue a GET request to https://{{mockid}}.mock.pstmn.io I get the following response:
{
"error": {
"name": "mockRequestNotFoundError",
"message": "We were unable to find any matching requests for the mock path (i.e. undefined) in your collection."
}
}
According to the same guide mentioned above the following url to "run the mock" https://{{mockId}}.mock.pstmn.io/{{mockPath}} but what exactly is mockPath?
Within my collection I have plenty of folders, and inside one of these folders I have a request with an example response. How do I access this example response through the mock? Thanks for all help in advance!
Here's the Postman Pro API, which doesnt mention a lot more than just creating reading mocks.
I had the same issue seeing an irrelevant error but finally I found the solution. Unfortunately I cannot find a reference in Postman website. But here is my solution:
When you create a Mock server you define your first request (like GET api/v1/about). So the Mock server will be created but even when you obtain your API key and put it in the header of request (as x-api-key) it still returns an error. It doesn't make sense but it turned out that defining the request is not enough. For me it only started returning a response when I added an Example for the request.
So I suggest for each request that you create, also create at least one example. The request you send will be matched with the examples you have created and the matched response will be returned. You can define body, headers and the HTTP status code of the example response..
I have no Pro Postman subscription and it worked for me using my free subscription.
Menu for adding an example or selecting one of them for editing:
UI for defining the example (See body, headers and status) :
How to go back to the request page:
Here is the correct reply I get based on my example:
If you request in the example is a GET on api.domain.com/api/foo then the mockPath is /api/foo and your mock endpoint is a GET call to https://{{mockid}}.mock.pstmn.io/api/foo.
The HTTP request methods and the the pathname as shown in the image below constitute a mock.
For ease of use the mock server is designed to be used on top of collections. The request in the examples is used as is along with response attached to it. The name of the folder or collection is not a part of the pathname and is not factored in anywhere when using a mock. Mocking a collection means mocking all the examples in within your collection. An example is a tuple of request and response.
An optional response status code if specified lets you fetch the appropriate response for the same path. This can be specified with the x-mock-response-code header. So passing x-mock-response-code as 404 will return the example that matches the pathname and has a response with status code of 404.
Currently if there are examples with the same path but different domains, and mock is unable to distinguish between them it will deterministically return the first one.
Also if you have several examples for the same query :
Mock request accept another optional header, x-mock-response-code, which specifies which integer response code your returned response should match. For example, 500 will return only a 500 response. If this header is not provided, the closest match of any response code will be returned.
Optional headers like x-mock-response-name or x-mock-response-id allow you to further specify the exact response you want by the name or by the uid of the saved example respectively.
Here's the documentation for more details.
{{mockPath}} is simply the path for your request. You should start by adding an example for any of your requests.
Example:
Request: https://www.google.com/path/to/my/api
After adding your mock server, you can access your examples at:
https://{{mockId}}.mock.pstmn.io/path/to/my/api

How to set a request header in behat tests?

I am developing a Rest API and testing it with Behat and mink-selenium2-driver (for the first time) . For security purposes, every call needs to contain a apikey in the request header.
My Problem is, i cannot set the header. My test looks like this:
Given I add "X_ApiKey" header equal to "test"
When I send a GET request to "/notice"
Then the response status code should be 200
But I keep getting a 403.
Any solutions?
In selenium it is imposible.
Need to test this on other driver, like guzzle
To my knowledge, selenium driver lead chrome, but not how it is working. Proposition to check of use others drivers like guzzle, where you can set headers is a answer, in my opinion.
No i found you could additionally other option. It's recommended you use a proxy to inject additional headers to the requests generated by the browser.
To do this i found
* http://wiremock.org/
You should use the behatch package which includes a behatch/rest context.
However, the selenium driver should only be used when you specifically need a browser, for javascript for example. In this case, as you are testing a API endpoint, using a browser will only slow you down and not bring any benefit.
It's possible to use Restler, a micro framework which can help with RESTful API testing in Behat. It support behavior Driven API testing using Behat and Guzzle.
Here is the example:
Given that "X_ApiKey" header is set to "test"
When I request "/notice"
Then the response status code should be 200
Here is another example from negotiation-format.feature file:
Scenario: One with more `q` should be selected, q = 1 when not defined
Given that "Accept" header is set to "application/json;q=0.8,application/xml"
When I request "/examples/_003_multiformat/bmi"
Then the response status code should be 200
And the response is XML
And the type is "array"