Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance; 16 : 17 : import static com.google.common.truth.Truth.assertThat; 18 : import static com.google.common.truth.Truth.assertWithMessage; 19 : import static com.google.gerrit.httpd.restapi.RestApiServlet.JSON_MAGIC; 20 : import static com.google.gerrit.httpd.restapi.RestApiServlet.SC_UNPROCESSABLE_ENTITY; 21 : import static java.nio.charset.StandardCharsets.UTF_8; 22 : import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; 23 : import static javax.servlet.http.HttpServletResponse.SC_CONFLICT; 24 : import static javax.servlet.http.HttpServletResponse.SC_CREATED; 25 : import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN; 26 : import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED; 27 : import static javax.servlet.http.HttpServletResponse.SC_MOVED_TEMPORARILY; 28 : import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; 29 : import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT; 30 : import static javax.servlet.http.HttpServletResponse.SC_OK; 31 : import static javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED; 32 : 33 : import java.io.IOException; 34 : import java.io.InputStreamReader; 35 : import java.io.Reader; 36 : import java.net.URI; 37 : 38 : public class RestResponse extends HttpResponse { 39 : 40 : RestResponse(org.apache.http.HttpResponse response) { 41 29 : super(response); 42 29 : } 43 : 44 : @Override 45 : public Reader getReader() throws IllegalStateException, IOException { 46 16 : if (reader == null && response.getEntity() != null) { 47 16 : reader = new InputStreamReader(response.getEntity().getContent(), UTF_8); 48 16 : reader.skip(JSON_MAGIC.length); 49 : } 50 16 : return reader; 51 : } 52 : 53 : public void assertStatus(int status) throws Exception { 54 27 : assertWithMessage(String.format("Expected status code %d", status)) 55 27 : .that(getStatusCode()) 56 27 : .isEqualTo(status); 57 27 : } 58 : 59 : public void assertOK() throws Exception { 60 17 : assertStatus(SC_OK); 61 17 : } 62 : 63 : public void assertNotFound() throws Exception { 64 10 : assertStatus(SC_NOT_FOUND); 65 10 : } 66 : 67 : public void assertConflict() throws Exception { 68 4 : assertStatus(SC_CONFLICT); 69 4 : } 70 : 71 : public void assertForbidden() throws Exception { 72 6 : assertStatus(SC_FORBIDDEN); 73 6 : } 74 : 75 : public void assertNoContent() throws Exception { 76 9 : assertStatus(SC_NO_CONTENT); 77 9 : } 78 : 79 : public void assertBadRequest() throws Exception { 80 8 : assertStatus(SC_BAD_REQUEST); 81 8 : } 82 : 83 : public void assertUnprocessableEntity() throws Exception { 84 2 : assertStatus(SC_UNPROCESSABLE_ENTITY); 85 2 : } 86 : 87 : public void assertMethodNotAllowed() throws Exception { 88 1 : assertStatus(SC_METHOD_NOT_ALLOWED); 89 1 : } 90 : 91 : public void assertCreated() throws Exception { 92 7 : assertStatus(SC_CREATED); 93 7 : } 94 : 95 : public void assertPreconditionFailed() throws Exception { 96 1 : assertStatus(SC_PRECONDITION_FAILED); 97 1 : } 98 : 99 : public void assertTemporaryRedirect(String path) throws Exception { 100 1 : assertStatus(SC_MOVED_TEMPORARILY); 101 1 : assertThat(URI.create(getHeader("Location")).getPath()).isEqualTo(path); 102 1 : } 103 : }