Fix python sdk tests refs #6833
[arvados.git] / sdk / go / arvadostest / stub.go
1 package arvadostest
2
3 import (
4         "net/http"
5 )
6
7 // StubResponse struct with response status and body
8 type StubResponse struct {
9         Status int
10         Body   string
11 }
12
13 // ServerStub with response map of path and StubResponse
14 // Ex:  /arvados/v1/keep_services = arvadostest.StubResponse{200, string(`{}`)}
15 type ServerStub struct {
16         Responses map[string]StubResponse
17 }
18
19 func (stub *ServerStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
20         if req.URL.Path == "/redirect-loop" {
21                 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
22                 return
23         }
24
25         pathResponse := stub.Responses[req.URL.Path]
26         if pathResponse.Status == -1 {
27                 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
28         } else if pathResponse.Body != "" {
29                 resp.WriteHeader(pathResponse.Status)
30                 resp.Write([]byte(pathResponse.Body))
31         } else {
32                 resp.WriteHeader(500)
33                 resp.Write([]byte(``))
34         }
35 }