Skip to content

Commit

Permalink
Merge branch '5.3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jan 12, 2022
2 parents 4b68dfb + 5fb58e5 commit 148d7ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.test.web.servlet.result;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
Expand All @@ -27,6 +28,7 @@

import org.springframework.core.style.ToStringCreator;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
Expand Down Expand Up @@ -248,7 +250,9 @@ protected void printResponse(MockHttpServletResponse response) throws Exception
this.printer.printValue("Error message", response.getErrorMessage());
this.printer.printValue("Headers", getResponseHeaders(response));
this.printer.printValue("Content type", response.getContentType());
this.printer.printValue("Body", response.getContentAsString());
String body = (MediaType.APPLICATION_JSON_VALUE.equals(response.getContentType()) ?
response.getContentAsString(StandardCharsets.UTF_8) : response.getContentAsString());
this.printer.printValue("Body", body);
this.printer.printValue("Forwarded URL", response.getForwardedUrl());
this.printer.printValue("Redirected URL", response.getRedirectedUrl());
printCookies(response.getCookies());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,10 +22,12 @@
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;

import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.result.PrintingResultHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -60,6 +62,22 @@ void printMvcResultsToWriter() throws Exception {
.contains("Headers = [Set-Cookie:\"enigma=42\", Content-Type:\"text/plain;charset=ISO-8859-1\", Content-Length:\"14\"]");
}

@Test
void printMvcResultsToWriterWithJsonResponseBodyInterpretedAsUtf8() throws Exception {
StringWriter writer = new StringWriter();

standaloneSetup(new SimpleController()).build()
// "Hallöchen" is German slang for "hello".
.perform(get("/utf8").accept(MediaType.APPLICATION_JSON).content("Hallöchen, Welt!".getBytes()).characterEncoding(UTF_8))
.andDo(print(writer))
// "Grüß dich!" is German for "greetings to you".
.andExpect(content().bytes("Grüß dich!".getBytes()));

assertThat(writer).asString()
.contains("Body = Hallöchen, Welt!")
.contains("Body = Grüß dich!");
}

@Test
void printMvcResultsToWriterWithFailingGlobalResultMatcher() throws Exception {
StringWriter writer = new StringWriter();
Expand Down Expand Up @@ -91,6 +109,11 @@ String hello(HttpServletResponse response) {
response.addCookie(new Cookie("enigma", "42"));
return "Hello Response";
}

@GetMapping("/utf8")
String utf8(HttpServletResponse response) {
return "Grüß dich!";
}
}

}

0 comments on commit 148d7ab

Please sign in to comment.