Arc-E-Tect
Arc-E-Tect

Reputation: 135

How to authenticate with a bearer token when using Spring Rest Docs

Can I use Spring RestDocs to document APIs when the authorization mode is bearer?

A pointer to a simple example would be appreciated, it doesn't seem the be available on the Spring RestDocs pages.

Thanks

Upvotes: 0

Views: 212

Answers (1)

Arc-E-Tect
Arc-E-Tect

Reputation: 135

It's actually very simple.

First obtain a bearer-token and encode it so you can use it as a string.
Next add the header to your call, like: .header("Authorization", "Bearer " + "your encoded token")

TestClient.put().uri("/v1/some_endpoint/foos")
                    .header("Authorization", "Bearer " + "your encoded token")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .body(BodyInserters.fromValue("{"pay":"load"}))
                    .exchange()
                    .expectStatus().isOk();

Upvotes: 0

Related Questions