/** * Verifies HTTP response status code against the provided matcher, * and throws {@link AssertionError} in case of mismatch. * @param matcher Matcher to validate status code * @return This object */ public RestResponse assertStatus(final Matcher<Integer> matcher) { MatcherAssert.assertThat( String.format( "HTTP response status is not the one expected:%n%s", this ), this.status(), matcher ); return this; }
@Override public MergeState merge( final String msg, final String sha) throws IOException { final JsonObjectBuilder builder = Json.createObjectBuilder() .add("commit_message", msg).add("sha", sha); final RestResponse response = this.merge(builder.build()) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_BAD_METHOD, HttpURLConnection.HTTP_CONFLICT ) ); final MergeState mergeState; switch (response.status()) { case HttpURLConnection.HTTP_OK: mergeState = MergeState.SUCCESS; break; case HttpURLConnection.HTTP_BAD_METHOD: mergeState = MergeState.NOT_MERGEABLE; break; default: mergeState = MergeState.BAD_HEAD; break; } return mergeState; }
@Override public boolean starred() throws IOException { return this.request .fetch().as(RestResponse.class) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_NO_CONTENT, HttpURLConnection.HTTP_NOT_FOUND ) ).status() == HttpURLConnection.HTTP_NO_CONTENT; }
@Override public boolean exists(final String path, final String ref) throws IOException { final RestResponse response = this.request.method(Request.GET) .uri().path(path).queryParam("ref", ref).back() .fetch().as(RestResponse.class); return response.status() == HttpURLConnection.HTTP_OK; }
@Override public boolean starred() throws IOException { return this.request.uri().path("star").back() .method("GET").fetch() .as(RestResponse.class).assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_NO_CONTENT, HttpURLConnection.HTTP_NOT_FOUND ) ).status() == HttpURLConnection.HTTP_NO_CONTENT; }
@Override public boolean isCollaborator( final String user) throws IOException { return this.request .method(Request.GET) .uri().path(user).back() .fetch() .as(RestResponse.class) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_NO_CONTENT, HttpURLConnection.HTTP_NOT_FOUND ) ).status() == HttpURLConnection.HTTP_NO_CONTENT; }
@Override public boolean check( final String login ) throws IOException { return this.request .method(Request.GET) .uri().path(login).back() .fetch() .as(RestResponse.class) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_NO_CONTENT, HttpURLConnection.HTTP_NOT_FOUND ) ).status() == HttpURLConnection.HTTP_NO_CONTENT; }
@Override public boolean contains( final User user ) throws IOException { return this.request .uri().path(user.login()).back() .method(Request.GET) .fetch().as(RestResponse.class) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_NO_CONTENT, HttpURLConnection.HTTP_NOT_FOUND ) ) .status() == HttpURLConnection.HTTP_NO_CONTENT; } }
@Override public void invite(final String friend) throws IOException { final RestResponse response = this.request .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='invite']/@href") .method(Request.POST) .body().formParam("name", friend).back() .fetch() .as(RestResponse.class); if (response.status() == HttpURLConnection.HTTP_MOVED_PERM && response.cookie(RtFriends.COOKIE_RS_FLASH).getValue() .startsWith("incorrect+alias") ) { throw new Friends.UnknownAliasException( response.cookie(RtFriends.COOKIE_RS_FLASH).getValue() ); } response.assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info(this, "friend '%s' invited", friend); }