Bastion is a library that eases the development of end-to-end tests for HTTP APIs. A typical user would write tests using Bastion based off of some sort of API specification. This API specification can be anything like a WADL file, RAML file or a JSON schema. A test engineer would prepare requests and responses based on these specifications to test the overall process of calling these APIs.
Reference guide can be found on: http://bastion-dev.github.io/Bastion/reference/index.html
JavaDocs are available on: http://bastion-dev.github.io/Bastion/javadocs/index.html
GET
Request:
Bastion.request("Get the Restaurant's Name", GeneralRequest.get("http://localhost:9876/restaurant"))
.withAssertions((statusCode, response, model) -> assertThat(model).isEqualTo("The Sushi Parlour"))
.call();
POST
Request:
Bastion.request("Change the Restaurant's Name", GeneralRequest.post("http://localhost:9876/restaurant", "The Fish Parlour"))
.withAssertions((statusCode, response, model) -> assertThat(model).isEqualTo("The Fish Parlour"))
.call();
JSON Assertion: (property order in response does not affect test)
Bastion.request("Get Nigiri Info", GeneralRequest.get("http://localhost:9876/nigiri"))
.withAssertions(JsonResponseAssertions.fromString(200, "{ \"id\":5, \"name\":\"Salmon Nigiri\", \"price\":23.55 }"))
.call();
JSON Request:
Bastion.request("First Request", JsonRequest.postFromString("http://localhost:9876/sushi", "{ " +
"\"name\":\"sashimi\", " +
"\"price\":\"5.60\", " +
"\"type\":\"SASHIMI\" " +
"}"
)).withAssertions(JsonResponseAssertions.fromString(201, "{ " +
"\"id\":5, " +
"\"name\":\"sashimi\", " +
"\"price\":5.60, " +
"\"type\":\"SASHIMI\" " +
"}"
).ignoreValuesForProperties("/id")
).call();
JSON Request/Assertion loaded from file:
Bastion.request("Create Sushi", JsonRequest.postFromResource(BASE_URL, "classpath:/json/create_sushi_request.json"))
.withAssertions(JsonResponseAssertions.fromResource(201, "classpath:/json/create_sushi_response.json").ignoreValuesForProperties("/id"))
.call();
Form URL Encoded Data Request:
Bastion.request("Order Sashimi", FormUrlEncodedRequest.post("http://localhost:9876/sashimi")
.addDataParameter("quantity", "5")
.addDataParameter("table", "61")
).withAssertions(JsonResponseAssertions.fromString(200, "{ \"id\":5, \"name\":\"Sashimi\", \"price\":5.95 }"))
.call();
Bind the response entity to a model object:
Sushi createdSushi = Bastion.request("Create Sushi", JsonRequest.postFromString("http://localhost:9876/sushi", "{ " +
"\"name\":\"sashimi\", " +
"\"price\":\"5.60\", " +
"\"type\":\"SASHIMI\" " +
"}"
)).bind(Sushi.class).withAssertions(JsonResponseAssertions.fromString(201, "{ " +
"\"id\":5, " +
"\"name\":\"sashimi\", " +
"\"price\":5.60, " +
"\"type\":\"SASHIMI\" " +
"}"
).ignoreValuesForProperties("id")
).call().getModel();
Groovy (using multi-line strings):
Bastion.request("First Request", JsonRequest.postFromString("http://localhost:9876/sushi",
'''{
"name":"sashimi",
"price":"5.60",
"type":"SASHIMI"
}'''
)).withAssertions(JsonResponseAssertions.fromString(201,
'''{
"id":5,
"name":"sashimi",
"price":5.60,
"type":"SASHIMI"
}'''
).ignoreValuesForProperties("/id")
).call()
Use a dependency management tool such as Maven to download the Bastion library for use in your project. Add the following dependency to your POM file:
<dependency>
<groupId>rocks.bastion</groupId>
<artifactId>bastion</artifactId>
<version>0.7-BETA</version>
<scope>test</scope>
</dependency>
Alternatively, use Groovy Grapes to use Bastion in your Groovy tests/scripts:
@Grapes(
@Grab(group='rocks.bastion', module='bastion', version='0.7-BETA')
)
Use Maven to build Bastion and run the associated tests. After checking out the repository use the following command to build and test the source code.
mvn install
Bastion is an open-source project! Open-source means that we encourage you to contribute in any way you can. We will accept all contributions, in any shape or form, that help make Bastion better. Here are some things you can do to contribute:
Request
or Assertions
implementations, for example, and you believe they could be useful to the rest of the Bastion community,
we will add them to the library for use in future versions of Bastion.README
better. If you feel like the README
is missing information which you think should be there then open a pull request
with your suggested changes. You can also help us with typos, grammar or anything else you see fit. Note that the front README
file is auto-generated: to submit a change please edit the README
source file.