|
| 1 | +// Copyright 2019, Google Inc. All rights reserved. |
| 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 | +using System.Collections.Generic; |
| 16 | +using System.Net; |
| 17 | +using System.Net.Http; |
| 18 | +using System.Text; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace FirebaseAdmin.Tests |
| 22 | +{ |
| 23 | + public class HttpErrorHandlerTest |
| 24 | + { |
| 25 | + public static readonly IEnumerable<object[]> HttpErrorCodes = |
| 26 | + new List<object[]>() |
| 27 | + { |
| 28 | + new object[] { HttpStatusCode.BadRequest, ErrorCode.InvalidArgument }, |
| 29 | + new object[] { HttpStatusCode.Unauthorized, ErrorCode.Unauthenticated }, |
| 30 | + new object[] { HttpStatusCode.Forbidden, ErrorCode.PermissionDenied }, |
| 31 | + new object[] { HttpStatusCode.NotFound, ErrorCode.NotFound }, |
| 32 | + new object[] { HttpStatusCode.Conflict, ErrorCode.Conflict }, |
| 33 | + new object[] { (HttpStatusCode)429, ErrorCode.ResourceExhausted }, |
| 34 | + new object[] { HttpStatusCode.InternalServerError, ErrorCode.Internal }, |
| 35 | + new object[] { HttpStatusCode.ServiceUnavailable, ErrorCode.Unavailable }, |
| 36 | + }; |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [MemberData(nameof(HttpErrorCodes))] |
| 40 | + public void KnownHttpStatusCode(HttpStatusCode statusCode, ErrorCode expected) |
| 41 | + { |
| 42 | + var json = "{}"; |
| 43 | + var resp = new HttpResponseMessage() |
| 44 | + { |
| 45 | + StatusCode = statusCode, |
| 46 | + Content = new StringContent(json, Encoding.UTF8, "application/json"), |
| 47 | + }; |
| 48 | + |
| 49 | + var handler = new HttpErrorHandler(); |
| 50 | + var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json)); |
| 51 | + |
| 52 | + Assert.Equal(expected, error.ErrorCode); |
| 53 | + Assert.Equal( |
| 54 | + $"Unexpected HTTP response with status: {(int)statusCode} ({statusCode})\n{json}", |
| 55 | + error.Message); |
| 56 | + Assert.Same(resp, error.HttpResponse); |
| 57 | + Assert.Null(error.InnerException); |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [MemberData(nameof(HttpErrorCodes))] |
| 62 | + public void NonJsonResponse(HttpStatusCode statusCode, ErrorCode expected) |
| 63 | + { |
| 64 | + var text = "not json"; |
| 65 | + var resp = new HttpResponseMessage() |
| 66 | + { |
| 67 | + StatusCode = statusCode, |
| 68 | + Content = new StringContent(text, Encoding.UTF8, "text/plain"), |
| 69 | + }; |
| 70 | + |
| 71 | + var handler = new HttpErrorHandler(); |
| 72 | + var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, text)); |
| 73 | + |
| 74 | + Assert.Equal(expected, error.ErrorCode); |
| 75 | + Assert.Equal( |
| 76 | + $"Unexpected HTTP response with status: {(int)statusCode} ({statusCode})\n{text}", |
| 77 | + error.Message); |
| 78 | + Assert.Same(resp, error.HttpResponse); |
| 79 | + Assert.Null(error.InnerException); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void UnknownHttpStatusCode() |
| 84 | + { |
| 85 | + var json = "{}"; |
| 86 | + var resp = new HttpResponseMessage() |
| 87 | + { |
| 88 | + StatusCode = HttpStatusCode.MethodNotAllowed, |
| 89 | + Content = new StringContent(json, Encoding.UTF8, "application/json"), |
| 90 | + }; |
| 91 | + |
| 92 | + var handler = new HttpErrorHandler(); |
| 93 | + var error = Assert.Throws<FirebaseException>(() => handler.ThrowIfError(resp, json)); |
| 94 | + |
| 95 | + Assert.Equal(ErrorCode.Unknown, error.ErrorCode); |
| 96 | + Assert.Equal( |
| 97 | + $"Unexpected HTTP response with status: 405 (MethodNotAllowed)\n{json}", |
| 98 | + error.Message); |
| 99 | + Assert.Same(resp, error.HttpResponse); |
| 100 | + Assert.Null(error.InnerException); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments