1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
11 "git.arvados.org/arvados.git/sdk/go/arvados"
14 // StubResponse struct with response status and body
15 type StubResponse struct {
20 // ServerStub with response map of path and StubResponse
21 // Ex: /arvados/v1/keep_services = arvadostest.StubResponse{200, string(`{}`)}
22 type ServerStub struct {
23 Responses map[string]StubResponse
26 func (stub *ServerStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
27 if req.URL.Path == "/redirect-loop" {
28 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
32 pathResponse := stub.Responses[req.URL.Path]
33 if pathResponse.Status == -1 {
34 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
35 } else if pathResponse.Body != "" {
36 resp.WriteHeader(pathResponse.Status)
37 resp.Write([]byte(pathResponse.Body))
40 resp.Write([]byte(``))
44 // SetServiceURL overrides the given service config/discovery with the
45 // given internalURLs.
47 // ExternalURL is set to the last internalURL, which only aims to
48 // address the case where there is only one.
50 // SetServiceURL panics on errors.
51 func SetServiceURL(service *arvados.Service, internalURLs ...string) {
52 service.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
53 for _, u := range internalURLs {
54 u, err := url.Parse(u)
58 service.InternalURLs[arvados.URL(*u)] = arvados.ServiceInstance{}
59 service.ExternalURL = arvados.URL(*u)