4

I'm failing to understand why this does not work. I'm assuming it's something simple that I'm overlooking. All other other test methods not utilizing a token work fine. There is no expiration currently on the token, and I can use it fine with Postman.

@Test
public void getUser() throws Exception {

    String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsd2lsbGlhbXMxNiIsInJvbGVzIjoidXNlciIsImlhdCI6MTUxNDQ0OTgzM30.WKMQ_oPPiDcc6sGtMJ1Y9hlrAAc6U3xQLuEHyAnM1FU";
    MvcResult mvcResult = mockMvc.perform(

            MockMvcRequestBuilders.get("/api/users/lwilliams16")
            .header("authentication", "Bearer " + token))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andDo(print())
            .andReturn();

    System.out.println(mvcResult.getResponse().getContentAsString());
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Leo Williams
  • 147
  • 1
  • 11

1 Answers1

4

I was utilizing the word Authentication instead of Authorization. It's late. Also, the correct response type is APPLICATION_JSON_UTF8.

@Test
public void getUser() throws Exception {

    String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsd2lsbGlhbXMxNiIsInJvbGVzIjoidXNlciIsImlhdCI6MTUxNDQ0OTgzM30.WKMQ_oPPiDcc6sGtMJ1Y9hlrAAc6U3xQLuEHyAnM1FU";
    MvcResult mvcResult = mockMvc.perform(

            MockMvcRequestBuilders.get("/api/users/lwilliams16")
            .header("authorization", "Bearer " + token))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(print())
            .andReturn();

    System.out.println(mvcResult.getResponse().getContentAsString());
}
Leo Williams
  • 147
  • 1
  • 11