14
14
15
15
using System ;
16
16
using System . Collections . Generic ;
17
+ using System . Net ;
17
18
using System . Net . Http ;
18
19
using System . Net . Sockets ;
20
+ using System . Text ;
19
21
using Xunit ;
20
22
21
23
namespace FirebaseAdmin . Tests
@@ -30,6 +32,7 @@ public void ErrorCodeAndMessage()
30
32
Assert . Equal ( ErrorCode . Internal , exception . ErrorCode ) ;
31
33
Assert . Equal ( "Test error message" , exception . Message ) ;
32
34
Assert . Null ( exception . InnerException ) ;
35
+ Assert . Null ( exception . HttpResponse ) ;
33
36
}
34
37
35
38
[ Fact ]
@@ -41,6 +44,20 @@ public void InnerException()
41
44
Assert . Equal ( ErrorCode . Internal , exception . ErrorCode ) ;
42
45
Assert . Equal ( "Test error message" , exception . Message ) ;
43
46
Assert . Same ( inner , exception . InnerException ) ;
47
+ Assert . Null ( exception . HttpResponse ) ;
48
+ }
49
+
50
+ [ Fact ]
51
+ public void HttpResponse ( )
52
+ {
53
+ var inner = new Exception ( "Inner exception" ) ;
54
+ var resp = new HttpResponseMessage ( ) ;
55
+ var exception = new FirebaseException ( ErrorCode . Internal , "Test error message" , inner , resp ) ;
56
+
57
+ Assert . Equal ( ErrorCode . Internal , exception . ErrorCode ) ;
58
+ Assert . Equal ( "Test error message" , exception . Message ) ;
59
+ Assert . Same ( inner , exception . InnerException ) ;
60
+ Assert . Same ( resp , exception . HttpResponse ) ;
44
61
}
45
62
46
63
[ Fact ]
@@ -54,6 +71,7 @@ public void TimeOutError()
54
71
Assert . Equal (
55
72
"Timed out while making an API call: Test error message" , exception . Message ) ;
56
73
Assert . Same ( httpError , exception . InnerException ) ;
74
+ Assert . Null ( exception . HttpResponse ) ;
57
75
}
58
76
59
77
[ Fact ]
@@ -78,6 +96,7 @@ public void NetworkUnavailableError()
78
96
Assert . Equal (
79
97
"Failed to establish a connection: Test error message" , exception . Message ) ;
80
98
Assert . Same ( httpError , exception . InnerException ) ;
99
+ Assert . Null ( exception . HttpResponse ) ;
81
100
}
82
101
}
83
102
@@ -92,6 +111,100 @@ public void UnknownLowLevelError()
92
111
"Unknown error while making a remote service call: Test error message" ,
93
112
exception . Message ) ;
94
113
Assert . Same ( httpError , exception . InnerException ) ;
114
+ Assert . Null ( exception . HttpResponse ) ;
115
+ }
116
+
117
+ [ Fact ]
118
+ public void PlatformError ( )
119
+ {
120
+ var json = @"{
121
+ ""error"": {
122
+ ""status"": ""UNAVAILABLE"",
123
+ ""message"": ""Test error message""
124
+ }
125
+ }" ;
126
+ var resp = new HttpResponseMessage ( )
127
+ {
128
+ StatusCode = HttpStatusCode . ServiceUnavailable ,
129
+ Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ,
130
+ } ;
131
+
132
+ var handler = new PlatformErrorHandler ( ) ;
133
+ var error = Assert . Throws < FirebaseException > ( ( ) => handler . ThrowIfError ( resp , json ) ) ;
134
+
135
+ Assert . Equal ( ErrorCode . Unavailable , error . ErrorCode ) ;
136
+ Assert . Equal ( "Test error message" , error . Message ) ;
137
+ Assert . Same ( resp , error . HttpResponse ) ;
138
+ Assert . Null ( error . InnerException ) ;
139
+ }
140
+
141
+ [ Fact ]
142
+ public void PlatformErrorWithoutCode ( )
143
+ {
144
+ var json = @"{
145
+ ""error"": {
146
+ ""message"": ""Test error message""
147
+ }
148
+ }" ;
149
+ var resp = new HttpResponseMessage ( )
150
+ {
151
+ StatusCode = HttpStatusCode . ServiceUnavailable ,
152
+ Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ,
153
+ } ;
154
+
155
+ var handler = new PlatformErrorHandler ( ) ;
156
+ var error = Assert . Throws < FirebaseException > ( ( ) => handler . ThrowIfError ( resp , json ) ) ;
157
+
158
+ Assert . Equal ( ErrorCode . Unavailable , error . ErrorCode ) ;
159
+ Assert . Equal ( "Test error message" , error . Message ) ;
160
+ Assert . Same ( resp , error . HttpResponse ) ;
161
+ Assert . Null ( error . InnerException ) ;
162
+ }
163
+
164
+ [ Fact ]
165
+ public void PlatformErrorWithoutMessage ( )
166
+ {
167
+ var json = @"{
168
+ ""error"": {
169
+ ""status"": ""INVALID_ARGUMENT"",
170
+ }
171
+ }" ;
172
+ var resp = new HttpResponseMessage ( )
173
+ {
174
+ StatusCode = HttpStatusCode . ServiceUnavailable ,
175
+ Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ,
176
+ } ;
177
+
178
+ var handler = new PlatformErrorHandler ( ) ;
179
+ var error = Assert . Throws < FirebaseException > ( ( ) => handler . ThrowIfError ( resp , json ) ) ;
180
+
181
+ Assert . Equal ( ErrorCode . InvalidArgument , error . ErrorCode ) ;
182
+ Assert . Equal (
183
+ $ "Unexpected HTTP response with status: 503 (ServiceUnavailable)\n { json } ",
184
+ error . Message ) ;
185
+ Assert . Same ( resp , error . HttpResponse ) ;
186
+ Assert . Null ( error . InnerException ) ;
187
+ }
188
+
189
+ [ Fact ]
190
+ public void PlatformErrorWithoutCodeOrMessage ( )
191
+ {
192
+ var json = @"{}" ;
193
+ var resp = new HttpResponseMessage ( )
194
+ {
195
+ StatusCode = HttpStatusCode . ServiceUnavailable ,
196
+ Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ,
197
+ } ;
198
+
199
+ var handler = new PlatformErrorHandler ( ) ;
200
+ var error = Assert . Throws < FirebaseException > ( ( ) => handler . ThrowIfError ( resp , json ) ) ;
201
+
202
+ Assert . Equal ( ErrorCode . Unavailable , error . ErrorCode ) ;
203
+ Assert . Equal (
204
+ "Unexpected HTTP response with status: 503 (ServiceUnavailable)\n {}" ,
205
+ error . Message ) ;
206
+ Assert . Same ( resp , error . HttpResponse ) ;
207
+ Assert . Null ( error . InnerException ) ;
95
208
}
96
209
}
97
210
}
0 commit comments