|
| 1 | +/* |
| 2 | + * Copyright 2020 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.internal; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertNull; |
| 22 | +import static org.junit.Assert.assertSame; |
| 23 | +import static org.junit.Assert.fail; |
| 24 | + |
| 25 | +import com.google.api.client.googleapis.util.Utils; |
| 26 | +import com.google.api.client.http.HttpStatusCodes; |
| 27 | +import com.google.api.client.http.LowLevelHttpRequest; |
| 28 | +import com.google.api.client.testing.http.MockHttpTransport; |
| 29 | +import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
| 30 | +import com.google.api.client.util.GenericData; |
| 31 | +import com.google.firebase.ErrorCode; |
| 32 | +import com.google.firebase.FirebaseException; |
| 33 | +import com.google.firebase.IncomingHttpResponse; |
| 34 | +import java.io.IOException; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class AbstractPlatformErrorHandlerTest { |
| 38 | + |
| 39 | + private static final HttpRequestInfo TEST_REQUEST = HttpRequestInfo.buildGetRequest( |
| 40 | + "https://firebase.google.com"); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testPlatformError() { |
| 44 | + String payload = "{\"error\": {\"status\": \"UNAVAILABLE\", \"message\": \"Test error\"}}"; |
| 45 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 46 | + .setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) |
| 47 | + .setContent(payload); |
| 48 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 49 | + |
| 50 | + try { |
| 51 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 52 | + fail("No exception thrown for HTTP error response"); |
| 53 | + } catch (FirebaseException e) { |
| 54 | + // TODO: Uncomment this line when getErrorCode is public |
| 55 | + // assertEquals(ErrorCode.UNAVAILABLE, e.getErrorCode()); |
| 56 | + assertEquals("Test error", e.getMessage()); |
| 57 | + assertHttpResponse(e, HttpStatusCodes.STATUS_CODE_SERVER_ERROR, payload); |
| 58 | + assertNotNull(e.getCause()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testNonJsonError() { |
| 64 | + String payload = "not json"; |
| 65 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 66 | + .setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) |
| 67 | + .setContent(payload); |
| 68 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 69 | + |
| 70 | + try { |
| 71 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 72 | + fail("No exception thrown for HTTP error response"); |
| 73 | + } catch (FirebaseException e) { |
| 74 | + // TODO: Uncomment this line when getErrorCode is public |
| 75 | + // assertEquals(ErrorCode.INTERNAL, e.getErrorCode()); |
| 76 | + assertEquals("Unexpected HTTP response with status: 500\nnot json", e.getMessage()); |
| 77 | + assertHttpResponse(e, HttpStatusCodes.STATUS_CODE_SERVER_ERROR, payload); |
| 78 | + assertNotNull(e.getCause()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testPlatformErrorWithoutCode() { |
| 84 | + String payload = "{\"error\": {\"message\": \"Test error\"}}"; |
| 85 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 86 | + .setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) |
| 87 | + .setContent(payload); |
| 88 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 89 | + |
| 90 | + try { |
| 91 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 92 | + fail("No exception thrown for HTTP error response"); |
| 93 | + } catch (FirebaseException e) { |
| 94 | + // TODO: Uncomment this line when getErrorCode is public |
| 95 | + // assertEquals(ErrorCode.INTERNAL, e.getErrorCode()); |
| 96 | + assertEquals("Test error", e.getMessage()); |
| 97 | + assertHttpResponse(e, HttpStatusCodes.STATUS_CODE_SERVER_ERROR, payload); |
| 98 | + assertNotNull(e.getCause()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testPlatformErrorWithoutMessage() { |
| 104 | + String payload = "{\"error\": {\"status\": \"INVALID_ARGUMENT\"}}"; |
| 105 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 106 | + .setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) |
| 107 | + .setContent(payload); |
| 108 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 109 | + |
| 110 | + try { |
| 111 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 112 | + fail("No exception thrown for HTTP error response"); |
| 113 | + } catch (FirebaseException e) { |
| 114 | + // TODO: Uncomment this line when getErrorCode is public |
| 115 | + // assertEquals(ErrorCode.INVALID_ARGUMENT, e.getErrorCode()); |
| 116 | + assertEquals("Unexpected HTTP response with status: 500\n" + payload, e.getMessage()); |
| 117 | + assertHttpResponse(e, HttpStatusCodes.STATUS_CODE_SERVER_ERROR, payload); |
| 118 | + assertNotNull(e.getCause()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testPlatformErrorWithoutCodeOrMessage() { |
| 124 | + String payload = "{}"; |
| 125 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 126 | + .setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) |
| 127 | + .setContent(payload); |
| 128 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 129 | + |
| 130 | + try { |
| 131 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 132 | + fail("No exception thrown for HTTP error response"); |
| 133 | + } catch (FirebaseException e) { |
| 134 | + // TODO: Uncomment this line when getErrorCode is public |
| 135 | + // assertEquals(ErrorCode.INTERNAL, e.getErrorCode()); |
| 136 | + assertEquals("Unexpected HTTP response with status: 500\n" + payload, e.getMessage()); |
| 137 | + assertHttpResponse(e, HttpStatusCodes.STATUS_CODE_SERVER_ERROR, payload); |
| 138 | + assertNotNull(e.getCause()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testNetworkError() { |
| 144 | + final IOException exception = new IOException("Test"); |
| 145 | + MockHttpTransport transport = new MockHttpTransport(){ |
| 146 | + @Override |
| 147 | + public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { |
| 148 | + throw exception; |
| 149 | + } |
| 150 | + }; |
| 151 | + ErrorHandlingHttpClient<FirebaseException> client = new ErrorHandlingHttpClient<>( |
| 152 | + transport.createRequestFactory(), |
| 153 | + Utils.getDefaultJsonFactory(), |
| 154 | + new TestPlatformErrorHandler()); |
| 155 | + |
| 156 | + try { |
| 157 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 158 | + fail("No exception thrown for HTTP error response"); |
| 159 | + } catch (FirebaseException e) { |
| 160 | + // TODO: Uncomment this line when getErrorCode is public |
| 161 | + // assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 162 | + assertEquals("IO error", e.getMessage()); |
| 163 | + assertNull(e.getHttpResponse()); |
| 164 | + assertSame(exception, e.getCause()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testParseError() { |
| 170 | + String payload = "not json"; |
| 171 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 172 | + .setContent(payload); |
| 173 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 174 | + |
| 175 | + try { |
| 176 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 177 | + fail("No exception thrown for HTTP error response"); |
| 178 | + } catch (FirebaseException e) { |
| 179 | + // TODO: Uncomment this line when getErrorCode is public |
| 180 | + // assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 181 | + assertEquals("Parse error", e.getMessage()); |
| 182 | + assertHttpResponse(e, HttpStatusCodes.STATUS_CODE_OK, payload); |
| 183 | + assertNotNull(e.getCause()); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testUnknownHttpError() { |
| 189 | + String payload = "{\"error\": {\"message\": \"Test error\"}}"; |
| 190 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
| 191 | + .setStatusCode(512) |
| 192 | + .setContent(payload); |
| 193 | + ErrorHandlingHttpClient<FirebaseException> client = createHttpClient(response); |
| 194 | + |
| 195 | + try { |
| 196 | + client.sendAndParse(TEST_REQUEST, GenericData.class); |
| 197 | + fail("No exception thrown for HTTP error response"); |
| 198 | + } catch (FirebaseException e) { |
| 199 | + // TODO: Uncomment this line when getErrorCode is public |
| 200 | + // assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 201 | + assertEquals("Test error", e.getMessage()); |
| 202 | + assertHttpResponse(e, 512, payload); |
| 203 | + assertNotNull(e.getCause()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private ErrorHandlingHttpClient<FirebaseException> createHttpClient( |
| 208 | + MockLowLevelHttpResponse response) { |
| 209 | + MockHttpTransport transport = new MockHttpTransport.Builder() |
| 210 | + .setLowLevelHttpResponse(response) |
| 211 | + .build(); |
| 212 | + return new ErrorHandlingHttpClient<>( |
| 213 | + transport.createRequestFactory(), |
| 214 | + Utils.getDefaultJsonFactory(), |
| 215 | + new TestPlatformErrorHandler()); |
| 216 | + } |
| 217 | + |
| 218 | + private void assertHttpResponse(FirebaseException e, int statusCode, String content) { |
| 219 | + IncomingHttpResponse httpResponse = e.getHttpResponse(); |
| 220 | + assertNotNull(httpResponse); |
| 221 | + assertEquals(statusCode, httpResponse.getStatusCode()); |
| 222 | + assertEquals(content, httpResponse.getContent()); |
| 223 | + assertEquals("GET", httpResponse.getRequest().getMethod()); |
| 224 | + } |
| 225 | + |
| 226 | + private static class TestPlatformErrorHandler extends |
| 227 | + AbstractPlatformErrorHandler<FirebaseException> { |
| 228 | + |
| 229 | + TestPlatformErrorHandler() { |
| 230 | + super(Utils.getDefaultJsonFactory()); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + protected FirebaseException createException(ErrorParams params) { |
| 235 | + return new FirebaseException(params.getErrorCode(), params.getMessage(), |
| 236 | + params.getResponse(), params.getException()); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public FirebaseException handleIOException(IOException e) { |
| 241 | + return new FirebaseException(ErrorCode.UNKNOWN, "IO error", null, e); |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public FirebaseException handleParseException(IOException e, IncomingHttpResponse response) { |
| 246 | + return new FirebaseException(ErrorCode.UNKNOWN, "Parse error", response, e); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments