Merge branch '8784-dir-listings'
[arvados.git] / sdk / go / arvadostest / stub.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvadostest
6
7 import (
8         "net/http"
9 )
10
11 // StubResponse struct with response status and body
12 type StubResponse struct {
13         Status int
14         Body   string
15 }
16
17 // ServerStub with response map of path and StubResponse
18 // Ex:  /arvados/v1/keep_services = arvadostest.StubResponse{200, string(`{}`)}
19 type ServerStub struct {
20         Responses map[string]StubResponse
21 }
22
23 func (stub *ServerStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
24         if req.URL.Path == "/redirect-loop" {
25                 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
26                 return
27         }
28
29         pathResponse := stub.Responses[req.URL.Path]
30         if pathResponse.Status == -1 {
31                 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
32         } else if pathResponse.Body != "" {
33                 resp.WriteHeader(pathResponse.Status)
34                 resp.Write([]byte(pathResponse.Body))
35         } else {
36                 resp.WriteHeader(500)
37                 resp.Write([]byte(``))
38         }
39 }