I’ve talked before about how Selenified supports web services and API testing, and as such, we are constantly working to simplify this experience, and expand out the support. One of the features included in the Selenified 3.0.1 release is the ability to pass in custom headers. Previously, all calls made had the Content-Type set to ‘application/json; charset=UTF-8.’ Below find out how to change that.

Custom Headers

Custom headers can be added to web-services calls, for whatever purpose. They can add user-agents, custom required headers for sites, or even override the default provided headers. By default, all web services calls are made with `Content-Type` set to `application/json; charset=UTF-8`. This can be changed by overridding this header. Headers can be added on a per test basis, or can be added for all tests in a suite. Headers should be set as key-value pairs, in a HashMap.

Map<String, String> headers = new HashMap<>();
headers.put("X-Atlassian-Token", "no-check");

To set this on an individual basis, simply retrieve the `Call` object, and add the headers

Call call = this.calls.get();
call.addHeaders(headers);

To set the headers for an entire class, in the `@BeforeMethod`, just call the static `addHeader` method.

addHeaders(this, test, headers);

Finally, if you want to reset the headers, on a test by test basis (maybe you want to set up headers for all tests instead of one), you can call the `resetHeaders` method.

Call call = this.calls.get();
call.resetHeaders();

Note that any headers set in the test, will override something set in the test suite. A full test suite might look something like this

public class ServicesOverrideIT extends Selenified {

    @BeforeClass(alwaysRun = true)
    public void beforeClass(ITestContext test) {
        // set the base URL for the tests here
        setTestSite(this, test, "https://jsonplaceholder.typicode.com/");
        // set the author of the tests here
        setAuthor(this, test, "Max Saperstone\n<br/>max.saperstone@coveros.com");
        // set the version of the tests or of the software, possibly with a
        // dynamic check
        setVersion(this, test, "3.0.2");
        // for this test, we want to change the default headers for each call
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/xml");
        addHeaders(this, test, headers);
        // for this particular test, we want to set some bogus credentials
        setCredentials(this, test, "servicesUsername", "servicesPassword");
    }

    @BeforeMethod(alwaysRun = true)
    protected void startTest(Object[] dataProvider, Method method, ITestContext test, ITestResult result) {
        super.startTest(dataProvider, method, test, result, DriverSetup.FALSE);
    }

    @Test(groups = {"integration", "services", "headers"},
            description = "An integration test to verify we can successfully set header values")
    public void addHeaderTest() {
        // use this object to verify the app looks as expected
        Call call = this.calls.get();
        //set some custom headers
        Map<String, String> headers = new HashMap<>();
        headers.put("X-Atlassian-Token", "no-check");
        call.addHeaders(headers);
        // perform some actions
        call.get("posts/").assertEquals(200);
        // verify no issues
        finish();
    }

    @Test(groups = {"integration", "services", "headers"},
            description = "An integration test to verify we can successfully override standard header values")
    public void overrideAcceptTest() {
        // use this object to verify the app looks as expected
        Call call = this.calls.get();
        //set some custom headers
        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "no-check");
        call.resetHeaders();
        call.addHeaders(headers);
        // perform some actions
        call.get("posts/").assertEquals(200);
        // verify no issues
        finish();
    }
}

Wrap Up

As you can see, this new functionality makes modifying how web services calls are made a snap.

Stay tuned for more posts on recent upgrades/updates made in the Selenified 3.0.1 release!

Leave a comment

Your email address will not be published. Required fields are marked *

X