I have a maven test error on github but on my local machine it works perfectly fine.
Error: Failures:
Error: AddPoliticianFilterTest.shouldReturn201CreatedIfAuthorizationIsCorrect:89 Status expected:<201> but was:<403>
The code for the test is:
@WebMvcTest(PoliticianController.class)
@AutoConfigureMockMvc(addFilters = true, printOnlyOnFailure = false,print = MockMvcPrint.DEFAULT)
public class AddPoliticianFilterTest {
@Autowired
public MockMvc mvc;
@MockBean
public PoliticiansService service;
private final String content = """
{
"name": "test name",
"rating": 9.00
}
""";
private Politicians politician;
@BeforeEach
public void setup() {
politician = new Politicians(1, 9.00D, "test name", List.of(new PoliticiansRating()), 9.00D);
}
@Test
public void shouldReturn201CreatedIfAuthorizationIsCorrect() throws URISyntaxException, Exception {
when(service.savePolitician(any())).thenReturn(politician);
mvc.perform(post(URI.create("/api/politicians/add-politician"))
.header("Politician-Access", "password")
.content(content)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("name",
equalTo("test name")))
.andExpect(jsonPath("rating",
equalTo(9.0)));
}
}
The filter that checks Headers if the expected header is present:
public class AddPoliticianFilter implements Filter{
//Should be changed to an environment variable
private final String password = "password";
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if (req.getRequestURI().equalsIgnoreCase("/api/politicians/add-politician")) {
if (req.getHeader("Politician-Access") != null) {
if (req.getHeader("Politician-Access").equalsIgnoreCase(password)) {
} else {
handleAddPoliticianAccessDenied(req, res);
return;
}
} else {
handleAddPoliticianAccessDenied(req, res);
return;
}
}
chain.doFilter(req, res);
}
private void handleAddPoliticianAccessDenied(HttpServletRequest req, HttpServletResponse res) throws JsonProcessingException, IOException {
ExceptionModel exceptionModel = new ExceptionModel();
exceptionModel.setCode("403");
exceptionModel.setErr("Authorization Required");
res.setStatus(403);
res.setContentType("application/json");
res.getWriter().write(new ObjectMapper().writeValueAsString(exceptionModel));
}
}
Machine Details: LSB Version: 1.4 Distributor ID: Arch Description: Arch Linux Release: rolling Codename: n/a
Comment From: wilkinsona
Thanks for the report but it's not clear why you think that this is a Spring Boot problem. Spring Boot doesn't do anything to change its behaviour when running on GitHub Actions so I suspect that this problem is caused by GitHub Actions rather than by Spring Boot.
You could try adding some debug logging to your filter and enabling it to see why the request is being rejected. You may also want to try using something like act
to see if you can reproduce the failure locally. If these suggestions don't help and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. We can then re-open the issue and take another look. You can share a simple with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.