REST Adapter
The REST adapter targets JSON APIs. It supports all HTTP verbs, request body serialization, bearer token authentication, typed response deserialization, and Promise<T> for passing data between steps.
Setup
- .NET
- Java
var rest = builder.RestAdapterBuilder()
.WithBaseUrl(new Uri("http://localhost:8080"))
.Build();
The builder is accessed via the TransitionBuilder extension method builder.RestAdapterBuilder() provided by Xcepto.Rest.
var rest = new RestAdapterBuilder(builder)
.withBaseUrl(URI.create("http://localhost:8080"))
.withSerializer(new GsonSerializer())
.build();
xceptoj does not bundle a JSON library. Supply a Serializer implementation. See Getting Started — Java.
HTTP verbs
- .NET
- Java
rest.Get("/resource")
rest.Post("/resource")
rest.Put("/resource")
rest.Patch("/resource")
rest.Delete("/resource")
All methods also accept a Func<string> for lazy path evaluation.
rest.get("/resource")
rest.post("/resource")
rest.request("/resource", HttpMethodVerb.PUT)
rest.request("/resource", HttpMethodVerb.PATCH)
rest.request("/resource", HttpMethodVerb.DELETE)
All methods also accept Supplier<String> for lazy path evaluation.
GET, PUT, DELETE are retried — evaluateConditionsForTransition is polled until all assertions pass or the timeout expires.
POST, PATCH execute once in onEnter — if an assertion fails, the test fails immediately.
Assertions
- .NET
- Java
rest.Get("/status")
.AssertSuccess() // 2xx
.AssertClientFailure() // 4xx
.AssertServerFailure() // 5xx
.AssertThatResponseStatus(
status => status == 201) // custom status
.AssertThatResponseContentString(
body => body.Contains("active")) // body predicate
.AssertThatResponse(response => { // full HttpResponseMessage
if (response.StatusCode != HttpStatusCode.OK)
throw new XceptoTestFailedException("Expected 200");
});
rest.get("/status")
.assertSuccess() // 2xx
.assertClientFailure() // 4xx
.assertServerFailure() // 5xx
.assertThatResponseStatus(
status -> status == 201) // custom status
.assertThatResponseContentString(
body -> body.contains("active")) // body predicate
.assertThatResponse(response -> { // full HttpResponse<String>
if (response.statusCode() != 200)
throw new XceptoTestFailedException("Expected 200");
});
Multiple assertions can be chained on the same step — all must pass for the state to transition.
Request body
- .NET
- Java
rest.Post("/shipment/accept")
.WithRequestBody(() => new AcceptShipmentRequest(50))
.AssertSuccess();
The supplier is evaluated lazily at execution time, not at step registration time.
rest.post("/shipment/accept")
.withRequestBody(() -> new AcceptShipmentRequest(50))
.assertSuccess();
Typed response deserialization
- .NET
- Java
rest.Post("/shipment/accept")
.WithRequestBody(() => new AcceptShipmentRequest(50))
.WithResponseType<AcceptShipmentResponse>()
.AssertThatResponse(r => r.Amount == 50);
WithResponseType<T>() deserializes the response body into T and adds a typed assertion overload.
rest.post("/shipment/accept")
.withRequestBody(() -> new AcceptShipmentRequest(50))
.withResponseType(AcceptShipmentResponse.class)
.assertThatResponse((AcceptShipmentResponse r) -> r.amount == 50);
When using .assertThatResponse after .withResponseType, provide an explicit parameter type in the lambda to disambiguate from the raw HttpResponseAssertion overload: (AcceptShipmentResponse r) -> ....
Bearer token authentication
- .NET
- Java
rest.Get("/api/me")
.WithBearerTokenClient(() => token)
.AssertSuccess();
rest.get("/api/me")
.withBearerTokenClient(() -> token)
.assertSuccess();
The token supplier is evaluated lazily — it can reference a Promise value resolved by a previous step. See Patterns → Promise.
Promise — typed response
Use promiseResponse() to capture a typed response and pass it to a later step:
- .NET
- Java
Promise<TokenResponse> token = rest.Post("/auth/login")
.WithRequestBody(() => new LoginRequest(username, password))
.WithResponseType<TokenResponse>()
.PromiseResponse();
rest.Get("/api/me")
.WithBearerTokenClient(() => token.Resolve().AccessToken)
.AssertSuccess();
Promise<TokenResponse> token = rest.post("/auth/login")
.withRequestBody(() -> new LoginRequest(username, password))
.withResponseType(TokenResponse.class)
.promiseResponse();
rest.get("/api/me")
.withBearerTokenClient(() -> token.resolve().accessToken)
.assertSuccess();
See Patterns → Promise for the full pattern.
Query parameters
- .NET
- Java
rest.Get("/search")
.AddQueryArgument(() => new[] { "q", searchTerm })
.AddQueryArgument(() => new[] { "limit", "10" })
.AssertSuccess();
rest.get("/search")
.addQueryArgument(() -> new String[]{"q", searchTerm})
.addQueryArgument(() -> new String[]{"limit", "10"})
.assertSuccess();