Posts Tagged: api

REST API Testing with Cucumber and Javascript

There are a great many tools available for testing REST Api’s by various vendors, open source plugins, browser plugins, and the old classic curl.

If you’re looking for a really simple way to test REST Api’s that is easy to maintain and entirely automated then you should consider using Cucumber with Javascript. If you’re not familiar with Cucumber, its an adapter that handles gherkin, a domain specific language for writing ATDD tests.

Here is what a simple automated test looks like using gherkin DSL:

Scenario: Making a simple GET request
    When I make a GET request to "/posts/1"
    Then The response property "userId" should be "1"
    And The response property "id" should be "1"

Any member of your team, whether it’s a developer, tester, or analyst can read and understand the scripts, which really makes it easier to collaborate as a team and eliminate confusion.

A simple POST request might look like this

Scenario: Making a POST request using tabular data
    Given The request data
    | title | body | userId |
    | foo   | bar  | 1      |
    When I make a POST request to "/posts"
    Then The response property "userId" should be "1"
    And The response property "id" should be "101"

Or if you prefer to use json (I certainly do), you can use this alternate format

Scenario: Making a POST request with json data
    Given The json request data
    """json
    {
        "title": "foo",
        "body": "bar",
        "userId": 1
    }
    """
    When I make a POST request to "/posts"
    Then The response property "userId" should be "1"
    And The response property "id" should be "101"

I have written a small framework as a starting point that you can clone from Github and start writing tests today. The framework can be customized as you go along and expanded to cover any testing need you have.

https://github.com/carbonrobot/relish

Working with Ranges in a REST API

A unique way of working with ranges in a more fluent way in a REST API. What is nice about this approach is its easy to hook up to javascript and is still a valid url scheme.

Simple search by month and year

/api/quote/count/by/month
/api/quote/count/by/month?year=2014
/api/quote/count/by/month?year=2014&month=4

Searching by year and inclusive months

/api/quote/count/by/month?year=2014&month=[1,2,3]

Searching by year and inclusive range of months (Jan – Sept)

/api/quote/count/by/month?year=2014&month=[1:9]

Searching by year and any month up to and including September

/api/quote/count/by/month?year=2014&month=[:9]

Searching by year and any month from February and after

/api/quote/count/by/month?year=2014&month=[2:]

Searching by year and specific months

/api/quote/count/by/month?year=2014&month=[7|10|11]