@@ -13,6 +13,16 @@ namespace FirebaseAdmin.Tests.Messaging
13
13
{
14
14
public class InstanceIdClientTest
15
15
{
16
+ public static readonly IEnumerable < object [ ] > ErrorCodes =
17
+ new List < object [ ] > ( )
18
+ {
19
+ new object [ ] { HttpStatusCode . BadRequest , ErrorCode . InvalidArgument } ,
20
+ new object [ ] { HttpStatusCode . Unauthorized , ErrorCode . Unauthenticated } ,
21
+ new object [ ] { HttpStatusCode . Forbidden , ErrorCode . PermissionDenied } ,
22
+ new object [ ] { HttpStatusCode . NotFound , ErrorCode . NotFound } ,
23
+ new object [ ] { HttpStatusCode . ServiceUnavailable , ErrorCode . Unavailable } ,
24
+ } ;
25
+
16
26
private static readonly GoogleCredential MockCredential =
17
27
GoogleCredential . FromAccessToken ( "test-token" ) ;
18
28
@@ -64,37 +74,14 @@ public async Task UnsubscribeFromTopicAsync()
64
74
Assert . Equal ( 1 , result . SuccessCount ) ;
65
75
}
66
76
67
- [ Fact ]
68
- public async Task BadRequest ( )
69
- {
70
- var handler = new MockMessageHandler ( )
71
- {
72
- StatusCode = HttpStatusCode . BadRequest ,
73
- Response = "BadRequest" ,
74
- } ;
75
- var factory = new MockHttpClientFactory ( handler ) ;
76
-
77
- var client = new InstanceIdClient ( factory , MockCredential ) ;
78
-
79
- var exception = await Assert . ThrowsAsync < FirebaseMessagingException > (
80
- ( ) => client . SubscribeToTopicAsync ( new List < string > { "abc123" } , "test-topic" ) ) ;
81
-
82
- Assert . Equal ( ErrorCode . InvalidArgument , exception . ErrorCode ) ;
83
- Assert . Equal (
84
- $ "Unexpected HTTP response with status: 400 (BadRequest){ Environment . NewLine } BadRequest",
85
- exception . Message ) ;
86
- Assert . Null ( exception . MessagingErrorCode ) ;
87
- Assert . NotNull ( exception . HttpResponse ) ;
88
- Assert . Null ( exception . InnerException ) ;
89
- }
90
-
91
- [ Fact ]
92
- public async Task Unauthorized ( )
77
+ [ Theory ]
78
+ [ MemberData ( nameof ( ErrorCodes ) ) ]
79
+ public async Task ErrorResponse ( HttpStatusCode statusCode , ErrorCode expected )
93
80
{
94
81
var handler = new MockMessageHandler ( )
95
82
{
96
- StatusCode = HttpStatusCode . Unauthorized ,
97
- Response = "Unauthorized ",
83
+ StatusCode = statusCode ,
84
+ Response = @"{""error"":""ErrorCode""} ",
98
85
} ;
99
86
var factory = new MockHttpClientFactory ( handler ) ;
100
87
@@ -103,22 +90,23 @@ public async Task Unauthorized()
103
90
var exception = await Assert . ThrowsAsync < FirebaseMessagingException > (
104
91
( ) => client . SubscribeToTopicAsync ( new List < string > { "abc123" } , "test-topic" ) ) ;
105
92
106
- Assert . Equal ( ErrorCode . Unauthenticated , exception . ErrorCode ) ;
93
+ Assert . Equal ( expected , exception . ErrorCode ) ;
107
94
Assert . Equal (
108
- $ "Unexpected HTTP response with status: 401 (Unauthorized) { Environment . NewLine } Unauthorized ",
95
+ "Error while calling the IID service: ErrorCode ",
109
96
exception . Message ) ;
110
97
Assert . Null ( exception . MessagingErrorCode ) ;
111
98
Assert . NotNull ( exception . HttpResponse ) ;
112
99
Assert . Null ( exception . InnerException ) ;
113
100
}
114
101
115
- [ Fact ]
116
- public async Task Forbidden ( )
102
+ [ Theory ]
103
+ [ MemberData ( nameof ( ErrorCodes ) ) ]
104
+ public async Task UnexpectedErrorResponse ( HttpStatusCode statusCode , ErrorCode expected )
117
105
{
118
106
var handler = new MockMessageHandler ( )
119
107
{
120
- StatusCode = HttpStatusCode . Forbidden ,
121
- Response = "Forbidden " ,
108
+ StatusCode = statusCode ,
109
+ Response = "NonJsonErrorResponse " ,
122
110
} ;
123
111
var factory = new MockHttpClientFactory ( handler ) ;
124
112
@@ -127,64 +115,16 @@ public async Task Forbidden()
127
115
var exception = await Assert . ThrowsAsync < FirebaseMessagingException > (
128
116
( ) => client . SubscribeToTopicAsync ( new List < string > { "abc123" } , "test-topic" ) ) ;
129
117
130
- Assert . Equal ( ErrorCode . PermissionDenied , exception . ErrorCode ) ;
118
+ Assert . Equal ( expected , exception . ErrorCode ) ;
131
119
Assert . Equal (
132
- $ "Unexpected HTTP response with status: 403 (Forbidden){ Environment . NewLine } Forbidden",
120
+ "Unexpected HTTP response with status: "
121
+ + $ "{ ( int ) statusCode } ({ statusCode } ){ Environment . NewLine } NonJsonErrorResponse",
133
122
exception . Message ) ;
134
123
Assert . Null ( exception . MessagingErrorCode ) ;
135
124
Assert . NotNull ( exception . HttpResponse ) ;
136
125
Assert . Null ( exception . InnerException ) ;
137
126
}
138
127
139
- [ Fact ]
140
- public async Task NotFound ( )
141
- {
142
- var handler = new MockMessageHandler ( )
143
- {
144
- StatusCode = HttpStatusCode . NotFound ,
145
- Response = "NotFound" ,
146
- } ;
147
- var factory = new MockHttpClientFactory ( handler ) ;
148
-
149
- var client = new InstanceIdClient ( factory , MockCredential ) ;
150
-
151
- var exception = await Assert . ThrowsAsync < FirebaseMessagingException > (
152
- ( ) => client . SubscribeToTopicAsync ( new List < string > { "abc123" } , "test-topic" ) ) ;
153
-
154
- Assert . Equal ( ErrorCode . NotFound , exception . ErrorCode ) ;
155
- Assert . Equal (
156
- $ "Unexpected HTTP response with status: 404 (NotFound){ Environment . NewLine } NotFound",
157
- exception . Message ) ;
158
- Assert . Null ( exception . MessagingErrorCode ) ;
159
- Assert . NotNull ( exception . HttpResponse ) ;
160
- Assert . Null ( exception . InnerException ) ;
161
- }
162
-
163
- [ Fact ]
164
- public async Task ServiceUnavailable ( )
165
- {
166
- var handler = new MockMessageHandler ( )
167
- {
168
- StatusCode = HttpStatusCode . ServiceUnavailable ,
169
- Response = "ServiceUnavailable" ,
170
- } ;
171
- var factory = new MockHttpClientFactory ( handler ) ;
172
-
173
- var client = new InstanceIdClient ( factory , MockCredential , RetryOptions . NoBackOff ) ;
174
-
175
- var exception = await Assert . ThrowsAsync < FirebaseMessagingException > (
176
- ( ) => client . SubscribeToTopicAsync ( new List < string > { "abc123" } , "test-topic" ) ) ;
177
-
178
- Assert . Equal ( ErrorCode . Unavailable , exception . ErrorCode ) ;
179
- Assert . Equal (
180
- $ "Unexpected HTTP response with status: 503 (ServiceUnavailable){ Environment . NewLine } ServiceUnavailable",
181
- exception . Message ) ;
182
- Assert . Null ( exception . MessagingErrorCode ) ;
183
- Assert . NotNull ( exception . HttpResponse ) ;
184
- Assert . Null ( exception . InnerException ) ;
185
- Assert . Equal ( 5 , handler . Calls ) ;
186
- }
187
-
188
128
[ Fact ]
189
129
public async Task TransportError ( )
190
130
{
0 commit comments