/** * Expect that the response status code matches an integer. * * @param expectedStatusCode The expected status code. * @return The builder */ public ResponseSpecBuilder expectStatusCode(int expectedStatusCode) { spec.statusCode(expectedStatusCode); return this; }
/** * Expect that the response content conforms to one or more Hamcrest matchers. * * @param matcher The hamcrest matcher that must response content must match. * @return The builder */ public ResponseSpecBuilder expectBody(Matcher<?> matcher) { spec.body(matcher); return this; }
/** * Set the response content type to be <code>contentType</code>. * <p>Note that this will affect the way the response is decoded. * E,g. if you can't use JSON/XML matching (see e.g. {@link #expectBody(String, org.hamcrest.Matcher)}) if you specify a * content-type of "text/plain". If you don't specify the response content type REST Assured will automatically try to * figure out which content type to use.</p> * * @param contentType The content type of the response. * @return The builder */ public ResponseSpecBuilder expectContentType(ContentType contentType) { spec.contentType(contentType); return this; }
@Test public void shouldAssambleRootResourcesXML() throws Exception { given(). then(). contentType(ROOT_XML_MEDIA_TYPE). body("root.link[0].@rel", equalTo("test")). body("root.link[0].@href", equalTo(new URL(baseURL, "api/test").toExternalForm())). when(). get(baseURL + "api/"); }
content(mapper.writeValueAsString(Lists.newArrayList("value1", "value2"))). expect(). statusCode(200). when(). post("/config/data/mykey"); statusCode(200). body("mykey",hasItems("value1","value2")). when(). get("/config/data/mykey"); statusCode(200). body("mykey.value",hasItems("value1","value2")). when(). get("/config/list"); statusCode(200). when(). delete("/config/data/mykey"); statusCode(404). when(). get("/config/data/mykey");
@Test( groups = {SHOULD}, description = "LDP clients SHOULD use the HTTP If-Match header and HTTP ETags " + "to ensure it isn’t modifying a resource that has changed since the " + "client last retrieved its representation. LDP servers SHOULD require " + "the HTTP If-Match header and HTTP ETags to detect collisions.") @SpecTest( specRefUri = LdpTestSuite.SPEC_URI + "#ldpr-put-precond", testMethod = METHOD.AUTOMATED, approval = STATUS.WG_APPROVED, comment = "Covers only part of the specification requirement. " + "testConditionFailedStatusCode, testPreconditionRequiredStatusCode " + "and testPutBadETag covers the rest.") public void testPutRequiresIfMatch() throws URISyntaxException { skipIfMethodNotAllowed(HttpMethod.PUT); String resourceUri = getResourceUri(); Response response = buildBaseRequestSpecification() .expect() .statusCode(isSuccessful()) .header(ETAG, isValidEntityTag()) .when() .get(resourceUri); buildBaseRequestSpecification() .contentType(response.getContentType()) .body(response.asByteArray()) .expect() .statusCode(not(isSuccessful())) .when() .put(resourceUri); }
protected void expectPut4xxConstrainedBy(String invalidProp) { Response putResponse = expectPut4xxStatus(invalidProp); final String uri = getResourceUri(); String constrainedBy = getFirstLinkForRelation(uri, LINK_REL_CONSTRAINEDBY, uri, putResponse); assertNotNull(constrainedBy, "Response did not contain a Link header with rel=\"http://www.w3.org/ns/ldp#constrainedBy\""); // Make sure we can GET the constrainedBy link. buildBaseRequestSpecification() .expect() .statusCode(isSuccessful()) .when() .get(constrainedBy); }
@Override public void perform() { given(). then(). statusCode(Status.UNAUTHORIZED.getStatusCode()). when(). delete(testURL.toExternalForm()); } }).inspect(new SetupAuth(null));
private String post(Model content, String slug) { RequestSpecification spec = buildBaseRequestSpecification().contentType(TEXT_TURTLE); if (slug != null) { spec.header(SLUG, slug); } Response post = spec.body(content, new RdfObjectMapper()).expect() .statusCode(HttpStatus.SC_CREATED).when() .post(getResourceUri()); String location = post.getHeader(LOCATION); assertNotNull(location, MSG_LOC_NOTFOUND); return location; }
@Override public void perform() { given(). then(). statusCode(Status.OK.getStatusCode()). when(). put(testURL.toExternalForm()); } }).inspect(new SetupAuth(new User("testuser")));
@Test public void testGetIcon_svgImageTypeFound() throws Exception { given().expect() // .statusCode(200).log().ifError() // .body(Matchers.containsString("</svg>")) // .contentType(IMAGE_SVG_VALUE) // .get(getVersionPrefix() + "/properties/{name}/icon/{type}", DATA_SET_DEFINITION_NAME, DefinitionImageType.SVG_ICON); }
protected static UserDTO assertUserExists(String path){ Response response = given() .expect().log().ifError() .statusCode(HttpStatus.SC_OK) .when() .get(getHost() + CONTEXT_PATH + path + JSON_SUFFIX); return deserialize(response.getBody().asString()); } }
protected static void assertCreateUser(User user){ given() .contentType("application/json;charset=UTF-8") .accept("application/json") .body(user) .expect().log().ifError() .statusCode(HttpStatus.SC_CREATED) .when() .post(getHost() + CONTEXT_PATH + "/users"); }
protected Response postNonRDFSource(String slug, String file, String mimeType) throws IOException { // Make sure we can post binary resources return buildBaseRequestSpecification() .header(SLUG, slug) .body(IOUtils.toByteArray(getClass().getResourceAsStream("/" + file))) .contentType(mimeType) .expect() .statusCode(HttpStatus.SC_CREATED) .header(LOCATION, HeaderMatchers.headerPresent()) .when() .post(container); }
private String getDataStoreDefinitionPropertiesTitle(Locale locale) throws IOException { Response response = given().accept(ServiceConstants.UI_SPEC_CONTENT_TYPE) // .expect() // .statusCode(200).log().ifError() // .with().port(localServerPort) // .param(LANGUAGE_QUERY_PARAMETER_NAME, locale.toLanguageTag()) .get(getVersionPrefix() + "/properties/{name}", DATA_STORE_DEFINITION_NAME); JsonNode jsonNode = mapper.readTree(response.asInputStream()); return jsonNode.get("jsonSchema").get("title").asText(); }
@Test( groups = {SHOULD}, dependsOnMethods = {"testPutToCreate"}, description = "LDP servers that allow LDPR creation via PUT SHOULD NOT " + "re-use URIs. For RDF representations (LDP-RSs),the created " + "resource can be thought of as an RDF named graph [rdf11-concepts].") @SpecTest( specRefUri = LdpTestSuite.SPEC_URI + "#ldpc-put-create", testMethod = METHOD.AUTOMATED, approval = STATUS.WG_APPROVED) public void testRestrictPutReUseUri() { String location = putToCreate(); // Delete the resource. buildBaseRequestSpecification() .expect() .statusCode(isSuccessful()) .when() .delete(location); // Try to put to the same URI again. It should fail. Model content = postContent(); buildBaseRequestSpecification() .given() .contentType(TEXT_TURTLE) .body(content, new RdfObjectMapper(location)) .expect() .statusCode(not(isSuccessful())) .when() .put(location); }
@Test public void testGetIcon_imageTypeNotFound() throws Exception { given().expect() // .statusCode(404) // .log().ifValidationFails() // .get(getVersionPrefix() + "/properties/{name}/icon/{type}", DATA_STORE_DEFINITION_NAME, DefinitionImageType.SVG_ICON); }
@Override public void perform() { responseValidation( given(). then(). contentType(getTypedMediaType()) , domain). when(). get(createRootURL() + "/{id}", domain.getId()). body(); } }).inspect(new SetupRepository<DOMAIN>(getDomainClass(), domain));
@Test public void validateDataStoreConnection() throws java.io.IOException { // given UiSpecsPropertiesDto properties = new UiSpecsPropertiesDto(); properties.setProperties(getJdbcDataStoreProperties()); // when given().content(properties).contentType(APPLICATION_JSON_UTF8_VALUE) // .accept(APPLICATION_JSON_UTF8_VALUE) // .expect().statusCode(200).log().ifError() // .post(getVersionPrefix() + "/runtimes/check"); }
public static Response sendHttpPost(String contentType, String payload, String httpPostCommand, boolean responseLog) { if (responseLog) { return given() .contentType(contentType) .body(payload) .log().all() .expect() .log().all() .when() .post(httpPostCommand); } return given() .contentType(contentType) .body(payload) // .log().all() .when() .post(httpPostCommand); }