-
Notifications
You must be signed in to change notification settings - Fork 53
Golem.Rest, Getting Started
=====
Golem includes an API built to execute and validate REST requests. It is built on top of RestSharp, and all rest sharp objects are available in the test. It uses a BDD-style syntax: Given, When, Then. This allows our tests to explicitly state which parts we are actually testing, and which parts are merely needed for set up. Additional methods can be added to the Given/When/Then classes using C# Extensions, or overriding the classes.
=====
The Given object contains function to set all request information: the Url, Verb, Headers and Body can all be set.
- public When When;
- public Given And;
- public Given Token(string token, string value)
- public Given Resource(string resource)
- public Given Header(string name, string value)
- public Given Domain(string domain)
- public Given Paramater(string name, string value)
- public Given File(string filePath)
- public Given Body(string bodyString)
- public Given Cookie(string name, string value)
The When object contains methods that will physically call the Service we are testing.
- public When(IRestClient client, IRestRequest request, IRestResponse response)
- public Then Then
- public When And
- public When Get(string resource = "")
- public When Post(string resource = "")
- public When Put(string resource = "")
- public When Delete(string resource = "")
- public When Options(string resource = "")
The Then object contains methods for dealing with the response. It contains methods to get data out of the response, as well as methods to verify the response was correct.
- public Then And
- public RestResponseVerify Verify(int timeoutSec=-1)
- public string Body
- public T GetBodyAs()
- public dynamic GetBodyAsDynamic()
- public string GetBodyAsString()
- public string GetStringFromBody(string xpath)
In this example I call /sqlrest/CUSTOMER to get a list of customers. I then parse a customer id out of the body and store it to a variable. I then use that variable in a second request to /sqlrest/CUSTOMER/ID and verify it is returning that ID's info.
public class Test : RestTestBase
{
[Test]
public void TestGetStringFromBody()
{
string id = Given.Domain("http://www.thomas-bayer.com")
.When.Get("/sqlrest/CUSTOMER")
.Then.GetStringFromBody("//CUSTOMER[10]/text()");
Given.Domain("http://www.thomas-bayer.com")
.When.Get("/sqlrest/CUSTOMER/" + id)
.Then.Verify().BodyContainsText("<ID>" + id + "</ID>");
}
}