Skip to content

Commit 77b5918

Browse files
author
Matt Raible
authored
Merge pull request oktadev#4 from jzheaux/gateway
Add Bearer Token to Requests
2 parents 4473ccc + 0b25c7e commit 77b5918

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

spring-cloud-gateway/car-service/src/test/java/com/example/carservice/CarServiceApplicationTests.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
package com.example.carservice;
22

3+
import java.time.LocalDate;
4+
import java.time.Month;
5+
import java.util.Collections;
6+
import java.util.UUID;
7+
import java.util.function.Consumer;
8+
39
import org.junit.Before;
410
import org.junit.Test;
511
import org.junit.runner.RunWith;
6-
import org.mockito.Mock;
12+
import reactor.core.publisher.Mono;
13+
714
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
915
import org.springframework.boot.test.context.SpringBootTest;
16+
import org.springframework.boot.test.mock.mockito.MockBean;
1017
import org.springframework.context.ApplicationContext;
18+
import org.springframework.context.annotation.Import;
19+
import org.springframework.http.HttpHeaders;
1120
import org.springframework.http.MediaType;
21+
import org.springframework.security.oauth2.jwt.Jwt;
22+
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
1223
import org.springframework.security.test.context.support.WithMockUser;
1324
import org.springframework.test.context.junit4.SpringRunner;
1425
import org.springframework.test.web.reactive.server.WebTestClient;
15-
import reactor.core.publisher.Mono;
16-
17-
import java.time.LocalDate;
18-
import java.time.Month;
19-
import java.util.Collections;
20-
import java.util.UUID;
2126

27+
import static org.mockito.ArgumentMatchers.anyString;
28+
import static org.mockito.Mockito.when;
2229
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
2330
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
2431

@@ -32,6 +39,9 @@ public class CarServiceApplicationTests {
3239
@Autowired
3340
CarRepository carRepository;
3441

42+
@MockBean
43+
ReactiveJwtDecoder jwtDecoder;
44+
3545
WebTestClient webTestClient;
3646

3747
@Before
@@ -45,11 +55,13 @@ public void setup() {
4555
}
4656

4757
@Test
48-
@WithMockUser
4958
public void testAddCar() {
5059
Car buggy = new Car(UUID.randomUUID(), "ID. BUGGY", LocalDate.of(2022, Month.DECEMBER, 1));
5160

61+
Jwt jwt = jwt();
62+
when(this.jwtDecoder.decode(anyString())).thenReturn(Mono.just(jwt));
5263
webTestClient.post().uri("/cars")
64+
.headers(addJwt(jwt))
5365
.contentType(MediaType.APPLICATION_JSON_UTF8)
5466
.accept(MediaType.APPLICATION_JSON_UTF8)
5567
.body(Mono.just(buggy), Car.class)
@@ -62,25 +74,39 @@ public void testAddCar() {
6274
}
6375

6476
@Test
65-
@WithMockUser
6677
public void testGetAllCars() {
78+
Jwt jwt = jwt();
79+
when(this.jwtDecoder.decode(anyString())).thenReturn(Mono.just(jwt));
6780
webTestClient.get().uri("/cars")
6881
.accept(MediaType.APPLICATION_JSON_UTF8)
82+
.headers(addJwt(jwt))
6983
.exchange()
7084
.expectStatus().isOk()
7185
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
7286
.expectBodyList(Car.class);
7387
}
7488

7589
@Test
76-
@WithMockUser
7790
public void testDeleteCar() {
7891
Car buzzCargo = carRepository.save(new Car(UUID.randomUUID(), "ID. BUZZ CARGO",
7992
LocalDate.of(2022, Month.DECEMBER, 2))).block();
8093

94+
Jwt jwt = jwt();
95+
when(this.jwtDecoder.decode(anyString())).thenReturn(Mono.just(jwt));
8196
webTestClient.delete()
8297
.uri("/cars/{id}", Collections.singletonMap("id", buzzCargo.getId()))
98+
.headers(addJwt(jwt))
8399
.exchange()
84100
.expectStatus().isOk();
85101
}
102+
103+
104+
private Jwt jwt() {
105+
return new Jwt("token", null, null,
106+
Collections.singletonMap("alg", "none"), Collections.singletonMap("sub", "dave"));
107+
}
108+
109+
private Consumer<HttpHeaders> addJwt(Jwt jwt) {
110+
return headers -> headers.setBearerAuth(jwt.getTokenValue());
111+
}
86112
}

0 commit comments

Comments
 (0)