|
1 | 1 | /* |
2 | | - * Copyright 2015-2021 the original author or authors. |
| 2 | + * Copyright 2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
27 | 27 |
|
28 | 28 | import org.springframework.beans.factory.annotation.Autowired; |
29 | 29 | import org.springframework.boot.test.context.SpringBootTest; |
30 | | -import org.springframework.restdocs.JUnitRestDocumentation; |
31 | 30 | import org.springframework.restdocs.RestDocumentationContextProvider; |
32 | 31 | import org.springframework.restdocs.RestDocumentationExtension; |
33 | 32 | import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; |
|
42 | 41 | */ |
43 | 42 | @SpringBootTest |
44 | 43 | @ExtendWith(RestDocumentationExtension.class) |
45 | | -public class WebIntegrationTests { |
| 44 | +class WebIntegrationTests { |
46 | 45 |
|
47 | | - public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); |
| 46 | + @Autowired WebApplicationContext context; |
| 47 | + @Autowired CustomerRepository customers; |
48 | 48 |
|
49 | | - @Autowired |
50 | | - WebApplicationContext context; |
51 | | - @Autowired |
52 | | - CustomerRepository customers; |
| 49 | + private MockMvc mvc; |
53 | 50 |
|
54 | | - private MockMvc mvc; |
| 51 | + @BeforeEach |
| 52 | + void setUp(RestDocumentationContextProvider restDocumentation) { |
55 | 53 |
|
56 | | - @BeforeEach |
57 | | - public void setUp(RestDocumentationContextProvider restDocumentation) { |
| 54 | + this.mvc = MockMvcBuilders.webAppContextSetup(context).// |
| 55 | + apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)).// |
| 56 | + build(); |
| 57 | + } |
58 | 58 |
|
59 | | - this.mvc = MockMvcBuilders.webAppContextSetup(context).// |
60 | | - apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)).// |
61 | | - build(); |
62 | | - } |
| 59 | + @Test |
| 60 | + void executeConditionalGetRequests() throws Exception { |
63 | 61 |
|
64 | | - @Test |
65 | | - public void executeConditionalGetRequests() throws Exception { |
| 62 | + var customer = customers.findAll().iterator().next(); |
| 63 | + var uri = new UriTemplate("/customers/{id}").expand(customer.getId()); |
66 | 64 |
|
67 | | - var customer = customers.findAll().iterator().next(); |
68 | | - var uri = new UriTemplate("/customers/{id}").expand(customer.getId()); |
| 65 | + var response = mvc.perform(get(uri)).// |
| 66 | + andExpect(header().string(ETAG, is(notNullValue()))).// |
| 67 | + andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
| 68 | + andReturn().getResponse(); |
69 | 69 |
|
70 | | - var response = mvc.perform(get(uri)).// |
71 | | - andExpect(header().string(ETAG, is(notNullValue()))).// |
72 | | - andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
73 | | - andReturn().getResponse(); |
| 70 | + // ETag-based |
74 | 71 |
|
75 | | - // ETag-based |
| 72 | + response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).// |
| 73 | + andExpect(status().isNotModified()).// |
| 74 | + andExpect(header().string(ETAG, is(notNullValue()))).// |
| 75 | + andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
| 76 | + andDo(document("if-none-match")).// |
| 77 | + andReturn().getResponse(); |
76 | 78 |
|
77 | | - response = mvc.perform(get(uri).header(IF_NONE_MATCH, response.getHeader(ETAG))).// |
78 | | - andExpect(status().isNotModified()).// |
79 | | - andExpect(header().string(ETAG, is(notNullValue()))).// |
80 | | - andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
81 | | - andDo(document("if-none-match")).// |
82 | | - andReturn().getResponse(); |
| 79 | + // Last-modified-based |
83 | 80 |
|
84 | | - // Last-modified-based |
85 | | - |
86 | | - mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).// |
87 | | - andExpect(status().isNotModified()).// |
88 | | - andExpect(header().string(ETAG, is(notNullValue()))).// |
89 | | - andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
90 | | - andDo(document("if-modified-since")); |
91 | | - } |
| 81 | + mvc.perform(get(uri).header(IF_MODIFIED_SINCE, response.getHeader(LAST_MODIFIED))).// |
| 82 | + andExpect(status().isNotModified()).// |
| 83 | + andExpect(header().string(ETAG, is(notNullValue()))).// |
| 84 | + andExpect(header().string(LAST_MODIFIED, is(notNullValue()))).// |
| 85 | + andDo(document("if-modified-since")); |
| 86 | + } |
92 | 87 | } |
0 commit comments