Java源码示例:io.restassured.config.EncoderConfig
示例1
@Test
public void testYamlContentType() throws Exception {
String artifactId = "testYamlContentType";
ArtifactType artifactType = ArtifactType.OPENAPI;
String artifactContent = resourceToString("openapi-empty.yaml");
// Create OpenAPI artifact (from YAML)
given()
.config(RestAssuredConfig.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs(CT_YAML, ContentType.TEXT)))
.when()
.contentType(CT_YAML)
.header("X-Registry-ArtifactId", artifactId)
.header("X-Registry-ArtifactType", artifactType.name())
.body(artifactContent)
.post("/artifacts")
.then()
.statusCode(200)
.body("id", equalTo(artifactId))
.body("name", equalTo("Empty API"))
.body("description", equalTo("An example API design using OpenAPI."))
.body("type", equalTo(artifactType.name()));
this.waitForArtifact(artifactId);
// Get the artifact content (should be JSON)
given()
.when()
.pathParam("artifactId", "testYamlContentType")
.get("/artifacts/{artifactId}")
.then()
.statusCode(200)
.header("Content-Type", Matchers.containsString(CT_JSON))
.body("openapi", equalTo("3.0.2"))
.body("info.title", equalTo("Empty API"));
}
示例2
@Test
public void testWsdlArtifact() throws Exception {
String artifactId = "testWsdlArtifact";
ArtifactType artifactType = ArtifactType.WSDL;
String artifactContent = resourceToString("sample.wsdl");
// Create OpenAPI artifact (from YAML)
given()
.config(RestAssuredConfig.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs(CT_XML, ContentType.TEXT)))
.when()
.contentType(CT_XML)
.header("X-Registry-ArtifactId", artifactId)
.header("X-Registry-ArtifactType", artifactType.name())
.body(artifactContent)
.post("/artifacts")
.then()
.statusCode(200)
.body("id", equalTo(artifactId))
.body("type", equalTo(artifactType.name()));
this.waitForArtifact(artifactId);
// Get the artifact content (should be XML)
given()
.when()
.pathParam("artifactId", "testWsdlArtifact")
.get("/artifacts/{artifactId}")
.then()
.statusCode(200)
.header("Content-Type", Matchers.containsString(CT_XML));
}
示例3
@Test
public void testNoopDefaultMapping() {
RestAssured.given()
.config(RestAssured.config.encoderConfig(
EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.contentType("")
.header("ce-id", UUID.randomUUID().toString())
.header("ce-type", "noop")
.header("ce-source", "test")
.post("/")
.then().statusCode(204)
.header("ce-id", nullValue())
.header("ce-type", nullValue())
.header("ce-source", nullValue());
}
示例4
@Test
public void testNoopDefaultMappingGet() {
RestAssured.given()
.config(RestAssured.config.encoderConfig(
EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.contentType("")
.header("ce-id", UUID.randomUUID().toString())
.header("ce-type", "noop")
.header("ce-source", "test")
.get("/")
.then().statusCode(204)
.header("ce-id", nullValue())
.header("ce-type", nullValue())
.header("ce-source", nullValue());
}
示例5
@Test
public void testNoopGet() {
RestAssured.given()
.config(RestAssured.config.encoderConfig(
EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.contentType("")
.get("/noop")
.then().statusCode(204)
.header("ce-id", nullValue())
.header("ce-type", nullValue())
.header("ce-source", nullValue());
}
示例6
@Test
public void testGlobalRuleApplicationProtobuf() throws Exception {
ArtifactType artifactType = ArtifactType.PROTOBUF;
String artifactContent = resourceToString("protobuf-invalid-syntax.proto");
// First, create an artifact without the rule installed. Should work.
createArtifact("testGlobalRuleApplicationProtobuf/API", artifactType, artifactContent);
// Add a global rule
Rule rule = new Rule();
rule.setType(RuleType.VALIDITY);
rule.setConfig(ValidityLevel.SYNTAX_ONLY.name());
given()
.when()
.contentType(CT_JSON).body(rule)
.post("/rules")
.then()
.statusCode(204)
.body(anything());
// Get the global rule (make sure it was created)
TestUtils.retry(() -> {
given()
.when()
.get("/rules/VALIDITY")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("type", equalTo("VALIDITY"))
.body("config", equalTo("SYNTAX_ONLY"));
});
// Try to create an artifact that is not valid - now it should fail.
String artifactId = "testGlobalRuleApplicationProtobuf/InvalidAPI";
given()
.config(RestAssured.config().encoderConfig(new EncoderConfig().encodeContentTypeAs(CT_PROTO, ContentType.TEXT)))
.when()
.contentType(CT_PROTO)
.header("X-Registry-ArtifactId", artifactId)
.header("X-Registry-ArtifactType", artifactType.name())
.body(artifactContent)
.post("/artifacts")
.then()
.statusCode(400)
.body("error_code", equalTo(400))
.body("message", equalTo("Syntax violation for Protobuf artifact."));
}
示例7
protected MockMvcRequestSpecification given() {
return RestAssuredMockMvc.given()
.config(RestAssuredMockMvcConfig.config().encoderConfig(new EncoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.config(RestAssuredMockMvcConfig.config().logConfig(LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails()));
}
示例8
protected MockMvcRequestSpecification given() {
return RestAssuredMockMvc.given()
.config(RestAssuredMockMvcConfig.config().encoderConfig(new EncoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.config(RestAssuredMockMvcConfig.config().logConfig(LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails()));
}