10
10
using InfluxDB . Client . Core . Test ;
11
11
using NUnit . Framework ;
12
12
using WireMock . RequestBuilders ;
13
+ using WireMock . ResponseBuilders ;
14
+ using WireMock . Server ;
15
+ using WireMock . Settings ;
13
16
14
17
namespace InfluxDB . Client . Test
15
18
{
@@ -105,9 +108,9 @@ public void LogLevelWithQueryString()
105
108
{
106
109
var writer = new StringWriter ( ) ;
107
110
Trace . Listeners . Add ( new TextWriterTraceListener ( writer ) ) ;
108
-
111
+
109
112
_client . SetLogLevel ( LogLevel . Headers ) ;
110
-
113
+
111
114
MockServer
112
115
. Given ( Request . Create ( ) . WithPath ( "/api/v2/write" ) . UsingPost ( ) )
113
116
. RespondWith ( CreateResponse ( "{}" ) ) ;
@@ -117,20 +120,20 @@ public void LogLevelWithQueryString()
117
120
writeApi . WriteRecord ( "b1" , "org1" , WritePrecision . Ns ,
118
121
"h2o_feet,location=coyote_creek water_level=1.0 1" ) ;
119
122
}
120
-
123
+
121
124
StringAssert . Contains ( "org=org1" , writer . ToString ( ) ) ;
122
125
StringAssert . Contains ( "bucket=b1" , writer . ToString ( ) ) ;
123
126
StringAssert . Contains ( "precision=ns" , writer . ToString ( ) ) ;
124
127
}
125
-
128
+
126
129
[ Test ]
127
130
public void LogLevelWithoutQueryString ( )
128
131
{
129
132
var writer = new StringWriter ( ) ;
130
133
Trace . Listeners . Add ( new TextWriterTraceListener ( writer ) ) ;
131
-
134
+
132
135
_client . SetLogLevel ( LogLevel . Basic ) ;
133
-
136
+
134
137
MockServer
135
138
. Given ( Request . Create ( ) . WithPath ( "/api/v2/write" ) . UsingPost ( ) )
136
139
. RespondWith ( CreateResponse ( "{}" ) ) ;
@@ -140,12 +143,12 @@ public void LogLevelWithoutQueryString()
140
143
writeApi . WriteRecord ( "b1" , "org1" , WritePrecision . Ns ,
141
144
"h2o_feet,location=coyote_creek water_level=1.0 1" ) ;
142
145
}
143
-
146
+
144
147
StringAssert . DoesNotContain ( "org=org1" , writer . ToString ( ) ) ;
145
148
StringAssert . DoesNotContain ( "bucket=b1" , writer . ToString ( ) ) ;
146
149
StringAssert . DoesNotContain ( "precision=ns" , writer . ToString ( ) ) ;
147
150
}
148
-
151
+
149
152
[ Test ]
150
153
public async Task UserAgentHeader ( )
151
154
{
@@ -155,11 +158,11 @@ public async Task UserAgentHeader()
155
158
156
159
await _client . GetAuthorizationsApi ( ) . FindAuthorizationByIdAsync ( "id" ) ;
157
160
158
- var request = MockServer . LogEntries . Last ( ) ;
161
+ var request = MockServer . LogEntries . Last ( ) ;
159
162
StringAssert . StartsWith ( "influxdb-client-csharp/3." , request . RequestMessage . Headers [ "User-Agent" ] . First ( ) ) ;
160
163
StringAssert . EndsWith ( ".0.0" , request . RequestMessage . Headers [ "User-Agent" ] . First ( ) ) ;
161
164
}
162
-
165
+
163
166
[ Test ]
164
167
public void TrailingSlashInUrl ( )
165
168
{
@@ -217,14 +220,14 @@ public void TrailingSlashInUrl()
217
220
request = MockServer . LogEntries . Last ( ) ;
218
221
Assert . AreEqual ( MockServerUrl + "/api/v2/write?org=org1&bucket=b1&precision=ns" ,
219
222
request . RequestMessage . AbsoluteUrl ) ;
220
-
223
+
221
224
Assert . True ( MockServer . LogEntries . Any ( ) ) ;
222
225
foreach ( var logEntry in MockServer . LogEntries )
223
226
{
224
227
StringAssert . StartsWith ( MockServerUrl + "/api/v2/" , logEntry . RequestMessage . AbsoluteUrl ) ;
225
228
}
226
229
}
227
-
230
+
228
231
[ Test ]
229
232
public void ProduceTypedException ( )
230
233
{
@@ -242,9 +245,45 @@ public void ProduceTypedException()
242
245
public void CreateService ( )
243
246
{
244
247
var service = _client . CreateService < DBRPsService > ( typeof ( DBRPsService ) ) ;
245
-
248
+
246
249
Assert . IsNotNull ( service ) ;
247
250
Assert . IsInstanceOf ( typeof ( DBRPsService ) , service ) ;
248
251
}
252
+
253
+ [ Test ]
254
+ public async Task RedirectToken ( )
255
+ {
256
+ _client . Dispose ( ) ;
257
+ _client = InfluxDBClientFactory . Create ( new InfluxDBClientOptions . Builder ( )
258
+ . Url ( MockServerUrl )
259
+ . AuthenticateToken ( "my-token" )
260
+ . AllowRedirects ( true )
261
+ . Build ( ) ) ;
262
+
263
+ var anotherServer = WireMockServer . Start ( new WireMockServerSettings
264
+ {
265
+ UseSSL = false
266
+ } ) ;
267
+
268
+ // redirect to another server
269
+ MockServer
270
+ . Given ( Request . Create ( ) . UsingGet ( ) )
271
+ . RespondWith ( Response . Create ( ) . WithStatusCode ( 301 ) . WithHeader ( "location" , anotherServer . Urls [ 0 ] ) ) ;
272
+
273
+
274
+ // success response
275
+ anotherServer
276
+ . Given ( Request . Create ( ) . UsingGet ( ) )
277
+ . RespondWith ( CreateResponse ( "{\" status\" :\" active\" }" , "application/json" ) ) ;
278
+
279
+ var authorization = await _client . GetAuthorizationsApi ( ) . FindAuthorizationByIdAsync ( "id" ) ;
280
+ Assert . AreEqual ( AuthorizationUpdateRequest . StatusEnum . Active , authorization . Status ) ;
281
+
282
+ StringAssert . StartsWith ( "Token my-token" ,
283
+ MockServer . LogEntries . Last ( ) . RequestMessage . Headers [ "Authorization" ] . First ( ) ) ;
284
+ Assert . False ( anotherServer . LogEntries . Last ( ) . RequestMessage . Headers . ContainsKey ( "Authorization" ) ) ;
285
+
286
+ anotherServer . Stop ( ) ;
287
+ }
249
288
}
250
289
}
0 commit comments