@@ -186,26 +186,26 @@ type Server struct {
186
186
calls int
187
187
}
188
188
189
- //Start() starts an unstarted ghttp server. It is a catastrophic error to call Start more than once (thanks, httptest).
189
+ // Start() starts an unstarted ghttp server. It is a catastrophic error to call Start more than once (thanks, httptest).
190
190
func (s * Server ) Start () {
191
191
s .HTTPTestServer .Start ()
192
192
}
193
193
194
- //URL() returns a url that will hit the server
194
+ // URL() returns a url that will hit the server
195
195
func (s * Server ) URL () string {
196
196
s .rwMutex .RLock ()
197
197
defer s .rwMutex .RUnlock ()
198
198
return s .HTTPTestServer .URL
199
199
}
200
200
201
- //Addr() returns the address on which the server is listening.
201
+ // Addr() returns the address on which the server is listening.
202
202
func (s * Server ) Addr () string {
203
203
s .rwMutex .RLock ()
204
204
defer s .rwMutex .RUnlock ()
205
205
return s .HTTPTestServer .Listener .Addr ().String ()
206
206
}
207
207
208
- //Close() should be called at the end of each test. It spins down and cleans up the test server.
208
+ // Close() should be called at the end of each test. It spins down and cleans up the test server.
209
209
func (s * Server ) Close () {
210
210
s .rwMutex .Lock ()
211
211
server := s .HTTPTestServer
@@ -217,14 +217,14 @@ func (s *Server) Close() {
217
217
}
218
218
}
219
219
220
- //ServeHTTP() makes Server an http.Handler
221
- //When the server receives a request it handles the request in the following order:
220
+ // ServeHTTP() makes Server an http.Handler
221
+ // When the server receives a request it handles the request in the following order:
222
222
//
223
- //1. If the request matches a handler registered with RouteToHandler, that handler is called.
224
- //2. Otherwise, if there are handlers registered via AppendHandlers, those handlers are called in order.
225
- //3. If all registered handlers have been called then:
226
- // a) If AllowUnhandledRequests is set to true, the request will be handled with response code of UnhandledRequestStatusCode
227
- // b) If AllowUnhandledRequests is false, the request will not be handled and the current test will be marked as failed.
223
+ // 1. If the request matches a handler registered with RouteToHandler, that handler is called.
224
+ // 2. Otherwise, if there are handlers registered via AppendHandlers, those handlers are called in order.
225
+ // 3. If all registered handlers have been called then:
226
+ // a) If AllowUnhandledRequests is set to true, the request will be handled with response code of UnhandledRequestStatusCode
227
+ // b) If AllowUnhandledRequests is false, the request will not be handled and the current test will be marked as failed.
228
228
func (s * Server ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
229
229
s .rwMutex .Lock ()
230
230
defer func () {
@@ -280,18 +280,18 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
280
280
}
281
281
}
282
282
283
- //ReceivedRequests is an array containing all requests received by the server (both handled and unhandled requests)
283
+ // ReceivedRequests is an array containing all requests received by the server (both handled and unhandled requests)
284
284
func (s * Server ) ReceivedRequests () []* http.Request {
285
285
s .rwMutex .RLock ()
286
286
defer s .rwMutex .RUnlock ()
287
287
288
288
return s .receivedRequests
289
289
}
290
290
291
- //RouteToHandler can be used to register handlers that will always handle requests that match
292
- //the passed in method and path.
291
+ // RouteToHandler can be used to register handlers that will always handle requests that match
292
+ // the passed in method and path.
293
293
//
294
- //The path may be either a string object or a *regexp.Regexp.
294
+ // The path may be either a string object or a *regexp.Regexp.
295
295
func (s * Server ) RouteToHandler (method string , path interface {}, handler http.HandlerFunc ) {
296
296
s .rwMutex .Lock ()
297
297
defer s .rwMutex .Unlock ()
@@ -337,25 +337,25 @@ func (s *Server) handlerForRoute(method string, path string) (http.HandlerFunc,
337
337
return nil , false
338
338
}
339
339
340
- //AppendHandlers will appends http.HandlerFuncs to the server's list of registered handlers. The first incoming request is handled by the first handler, the second by the second, etc...
340
+ // AppendHandlers will appends http.HandlerFuncs to the server's list of registered handlers. The first incoming request is handled by the first handler, the second by the second, etc...
341
341
func (s * Server ) AppendHandlers (handlers ... http.HandlerFunc ) {
342
342
s .rwMutex .Lock ()
343
343
defer s .rwMutex .Unlock ()
344
344
345
345
s .requestHandlers = append (s .requestHandlers , handlers ... )
346
346
}
347
347
348
- //SetHandler overrides the registered handler at the passed in index with the passed in handler
349
- //This is useful, for example, when a server has been set up in a shared context, but must be tweaked
350
- //for a particular test.
348
+ // SetHandler overrides the registered handler at the passed in index with the passed in handler
349
+ // This is useful, for example, when a server has been set up in a shared context, but must be tweaked
350
+ // for a particular test.
351
351
func (s * Server ) SetHandler (index int , handler http.HandlerFunc ) {
352
352
s .rwMutex .Lock ()
353
353
defer s .rwMutex .Unlock ()
354
354
355
355
s .requestHandlers [index ] = handler
356
356
}
357
357
358
- //GetHandler returns the handler registered at the passed in index.
358
+ // GetHandler returns the handler registered at the passed in index.
359
359
func (s * Server ) GetHandler (index int ) http.HandlerFunc {
360
360
s .rwMutex .RLock ()
361
361
defer s .rwMutex .RUnlock ()
@@ -374,12 +374,12 @@ func (s *Server) Reset() {
374
374
s .routedHandlers = nil
375
375
}
376
376
377
- //WrapHandler combines the passed in handler with the handler registered at the passed in index.
378
- //This is useful, for example, when a server has been set up in a shared context but must be tweaked
379
- //for a particular test.
377
+ // WrapHandler combines the passed in handler with the handler registered at the passed in index.
378
+ // This is useful, for example, when a server has been set up in a shared context but must be tweaked
379
+ // for a particular test.
380
380
//
381
- //If the currently registered handler is A, and the new passed in handler is B then
382
- //WrapHandler will generate a new handler that first calls A, then calls B, and assign it to index
381
+ // If the currently registered handler is A, and the new passed in handler is B then
382
+ // WrapHandler will generate a new handler that first calls A, then calls B, and assign it to index
383
383
func (s * Server ) WrapHandler (index int , handler http.HandlerFunc ) {
384
384
existingHandler := s .GetHandler (index )
385
385
s .SetHandler (index , CombineHandlers (existingHandler , handler ))
@@ -392,34 +392,55 @@ func (s *Server) CloseClientConnections() {
392
392
s .HTTPTestServer .CloseClientConnections ()
393
393
}
394
394
395
- //SetAllowUnhandledRequests enables the server to accept unhandled requests.
395
+ // SetAllowUnhandledRequests enables the server to accept unhandled requests.
396
396
func (s * Server ) SetAllowUnhandledRequests (allowUnhandledRequests bool ) {
397
397
s .rwMutex .Lock ()
398
398
defer s .rwMutex .Unlock ()
399
399
400
400
s .AllowUnhandledRequests = allowUnhandledRequests
401
401
}
402
402
403
- //GetAllowUnhandledRequests returns true if the server accepts unhandled requests.
403
+ // GetAllowUnhandledRequests returns true if the server accepts unhandled requests.
404
404
func (s * Server ) GetAllowUnhandledRequests () bool {
405
405
s .rwMutex .RLock ()
406
406
defer s .rwMutex .RUnlock ()
407
407
408
408
return s .AllowUnhandledRequests
409
409
}
410
410
411
- //SetUnhandledRequestStatusCode status code to be returned when the server receives unhandled requests
411
+ // SetUnhandledRequestStatusCode status code to be returned when the server receives unhandled requests
412
412
func (s * Server ) SetUnhandledRequestStatusCode (statusCode int ) {
413
413
s .rwMutex .Lock ()
414
414
defer s .rwMutex .Unlock ()
415
415
416
416
s .UnhandledRequestStatusCode = statusCode
417
417
}
418
418
419
- //GetUnhandledRequestStatusCode returns the current status code being returned for unhandled requests
419
+ // GetUnhandledRequestStatusCode returns the current status code being returned for unhandled requests
420
420
func (s * Server ) GetUnhandledRequestStatusCode () int {
421
421
s .rwMutex .RLock ()
422
422
defer s .rwMutex .RUnlock ()
423
423
424
424
return s .UnhandledRequestStatusCode
425
425
}
426
+
427
+ // RoundTripper returns a RoundTripper which updates requests to point to the server.
428
+ // This is useful when you want to use the server as a RoundTripper in an http.Client.
429
+ // If rt is nil, http.DefaultTransport is used.
430
+ func (s * Server ) RoundTripper (rt http.RoundTripper ) http.RoundTripper {
431
+ if rt == nil {
432
+ rt = http .DefaultTransport
433
+ }
434
+ return RoundTripperFunc (func (r * http.Request ) (* http.Response , error ) {
435
+ r .URL .Scheme = "http"
436
+ r .URL .Host = s .Addr ()
437
+ return rt .RoundTrip (r )
438
+ })
439
+ }
440
+
441
+ // Helper type for creating a RoundTripper from a function
442
+ type RoundTripperFunc func (* http.Request ) (* http.Response , error )
443
+
444
+ func (fn RoundTripperFunc ) RoundTrip (r * http.Request ) (* http.Response , error ) {
445
+ return fn (r )
446
+ }
0 commit comments