1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// Copyright (c) 2019-2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
package client
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
jsonresp "github.com/sylabs/json-resp"
)
type MockVersion struct {
t *testing.T
code int
message string
wantPath string
version string
}
func (m *MockVersion) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if m.code/100 != 2 { // non-2xx status code
if err := jsonresp.WriteError(w, m.message, m.code); err != nil {
m.t.Fatalf("failed to write error: %v", err)
}
return
}
if got, want := r.URL.Path, m.wantPath; got != want {
m.t.Errorf("got path %v, want %v", got, want)
}
if got, want := r.ContentLength, int64(0); got != want {
m.t.Errorf("got content length %v, want %v", got, want)
}
vi := struct {
Version string `json:"version"`
}{
Version: m.version,
}
if err := jsonresp.WriteResponse(w, vi, m.code); err != nil {
m.t.Fatalf("failed to write response: %v", err)
}
}
func TestGetVersion(t *testing.T) {
cancelled, cancel := context.WithCancel(context.Background())
cancel()
tests := []struct {
name string
path string
ctx context.Context
code int
message string
wantPath string
version string
wantErr error
}{
{
name: "OK",
ctx: context.Background(),
code: http.StatusOK,
wantPath: "/version",
version: "1.2.3",
},
{
name: "OKWithPath",
path: "/path",
ctx: context.Background(),
code: http.StatusOK,
wantPath: "/path/version",
version: "1.2.3",
},
{
name: "NonAuthoritativeInfo",
ctx: context.Background(),
code: http.StatusNonAuthoritativeInfo,
wantPath: "/version",
version: "1.2.3",
},
{
name: "HTTPError",
ctx: context.Background(),
code: http.StatusBadRequest,
wantErr: &HTTPError{code: http.StatusBadRequest},
},
{
name: "HTTPErrorMessage",
ctx: context.Background(),
code: http.StatusBadRequest,
message: "blah",
wantErr: &HTTPError{code: http.StatusBadRequest},
},
{
name: "ContextCanceled",
ctx: cancelled,
code: http.StatusOK,
wantErr: context.Canceled,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
s := httptest.NewServer(&MockVersion{
t: t,
code: tt.code,
message: tt.message,
wantPath: tt.wantPath,
version: tt.version,
})
defer s.Close()
c, err := NewClient(OptBaseURL(s.URL + tt.path))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
v, err := c.GetVersion(tt.ctx)
if got, want := err, tt.wantErr; !errors.Is(got, want) {
t.Fatalf("got error %v, want %v", got, want)
}
if err == nil {
if got, want := v, tt.version; got != want {
t.Errorf("got version %v, want %v", got, want)
}
}
})
}
}
|