-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package integration; | ||
|
||
import com.jayway.restassured.RestAssured; | ||
import com.jayway.restassured.response.Response; | ||
import com.xebialabs.restito.server.StubServer; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import static com.jayway.restassured.RestAssured.expect; | ||
import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp; | ||
import static com.xebialabs.restito.semantics.Action.*; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class CharsetAndEncodingTest { | ||
|
||
private StubServer server; | ||
|
||
private static Charset CP_1251 = Charset.forName("Cp1251"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mkotsur
Author
Owner
|
||
|
||
@Before | ||
public void start() { | ||
server = new StubServer().run(); | ||
RestAssured.port = server.getPort(); | ||
} | ||
|
||
@After | ||
public void stop() { | ||
server.stop(); | ||
} | ||
|
||
|
||
@Test | ||
public void shouldEncodeStringsUsingDefaultPlatformCharset() { | ||
whenHttp(server) | ||
.match("/default-charset"). | ||
then(stringContent("Хеллоу")); | ||
|
||
expect().header("Content-Type", is(nullValue())) | ||
.content(equalTo("Хеллоу")) | ||
This comment has been minimized.
Sorry, something went wrong.
garretwilson
|
||
.when().get("/default-charset"); | ||
} | ||
|
||
@Test | ||
public void shouldEncodeStringsUsingExplicitlyPassedCharset() { | ||
whenHttp(server) | ||
.match("/custom-charset-1"). | ||
then(contentType("application/text"), stringContent("Хеллоу", CP_1251)); | ||
|
||
whenHttp(server) | ||
.match("/custom-charset-2"). | ||
then(stringContent("Хеллоу", CP_1251), contentType("application/text")); | ||
|
||
expect().header("Content-Type", is("application/text;charset=windows-1251")) | ||
.content(equalTo("Хеллоу")) | ||
.when().get("/custom-charset-1"); | ||
|
||
expect().header("Content-Type", is("application/text;charset=windows-1251")) | ||
.content(equalTo("Хеллоу")) | ||
.when().get("/custom-charset-2"); | ||
} | ||
|
||
@Test | ||
public void shouldNotIncludeExplicitlyPassedInHeadersCharsetWhenContentTypeHeaderIsMissing() { | ||
whenHttp(server) | ||
.match("/custom-charset-3"). | ||
then(stringContent("Хеллоу", CP_1251)); | ||
|
||
Response response = expect().header("Content-Type", is(nullValue())) | ||
.when().get("/custom-charset-1"); | ||
|
||
assertArrayEquals(response.body().asByteArray(), "Хеллоу".getBytes(CP_1251)); | ||
} | ||
|
||
|
||
} |
Do you really know for sure that
Cp1251
is going to be available on all JVMs on all platforms? See https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html#forName-java.lang.String-There are the only ones guaranteed to be present: https://docs.oracle.com/javase/8/docs/api/java/nio/charset/StandardCharsets.html
And even if you stick with
Cp1251
, I would consider testing UTF-8 as well, as that's what 90% of people will use, I would think.