How to set request header in behavior tests?

I am developing a Rest API and testing it with Behat and mink-selenium2-driver (for the first time). For security reasons, each call should contain apikey in the request header.

My problem is that I cannot set the title. My test is as follows:

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 403.

Any solutions?

+5
source share
3 answers

In selenium, this is impossible. You need to check it on another driver, for example, click

As far as I know, the lead chrome selenium driver is selenium, but not how it works. The suggestion to check the use of other drivers, such as guzzle, where you can set the headers, is the answer in my opinion.

No, I found that you can additionally use another option. He recommended using a proxy server to enter additional headers in requests created by the browser.

For this, I found * http://wiremock.org/

+2
source

You should use the behatch package, which includes the behatch/rest context.

However, the selenium driver should only be used when you definitely need a browser, for example, for javascript. In this case, when you test the API endpoint, using the browser will only slow you down and will not do any good.

0
source

You can use Restler , a microarchitecture that can help with testing RESTful APIs in Behat. It supports Driven API testing using Behat and Guzzle.

Here is an 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" 
0
source

Source: https://habr.com/ru/post/1203368/


All Articles