| 1 | package tq |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/git-lfs/git-lfs/v3/lfsapi" |
| 14 | "github.com/git-lfs/git-lfs/v3/lfshttp" |
| 15 | "github.com/stretchr/testify/assert" |
| 16 | "github.com/stretchr/testify/require" |
| 17 | "github.com/xeipuuv/gojsonschema" |
| 18 | ) |
| 19 | |
| 20 | func TestAPIBatch(t *testing.T) { |
| 21 | require.NotNil(t, batchReqSchema, batchReqSchema.Source) |
| 22 | require.NotNil(t, batchResSchema, batchResSchema.Source) |
| 23 | |
| 24 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 | if r.URL.Path != "/api/objects/batch" { |
| 26 | w.WriteHeader(404) |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | assert.Equal(t, "POST", r.Method) |
| 31 | assert.Equal(t, "106", r.Header.Get("Content-Length")) |
| 32 | |
| 33 | bodyLoader, body := gojsonschema.NewReaderLoader(r.Body) |
| 34 | bReq := &batchRequest{} |
| 35 | err := json.NewDecoder(body).Decode(bReq) |
| 36 | r.Body.Close() |
| 37 | assert.Nil(t, err) |
| 38 | assertSchema(t, batchReqSchema, bodyLoader) |
| 39 | |
| 40 | assert.EqualValues(t, []string{"basic", "whatev"}, bReq.TransferAdapterNames) |
| 41 | if assert.Equal(t, 1, len(bReq.Objects)) { |
| 42 | assert.Equal(t, "a", bReq.Objects[0].Oid) |
| 43 | } |
| 44 | |
| 45 | w.Header().Set("Content-Type", "application/json") |
| 46 | |
| 47 | writeLoader, resWriter := gojsonschema.NewWriterLoader(w) |
| 48 | err = json.NewEncoder(resWriter).Encode(&BatchResponse{ |
| 49 | TransferAdapterName: "basic", |
| 50 | Objects: bReq.Objects, |
| 51 | }) |
| 52 | |
| 53 | assert.Nil(t, err) |
| 54 | assertSchema(t, batchResSchema, writeLoader) |
| 55 | })) |
| 56 | defer srv.Close() |
| 57 | |
| 58 | c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{ |
| 59 | "lfs.url": srv.URL + "/api", |
| 60 | })) |
| 61 | |
| 62 | tqc := &tqClient{Client: c} |
| 63 | bReq := &batchRequest{ |
| 64 | TransferAdapterNames: []string{"basic", "whatev"}, |
| 65 | Objects: []*Transfer{ |
| 66 | &Transfer{Oid: "a", Size: 1}, |
| 67 | }, |
| 68 | } |
| 69 | bRes, err := tqc.Batch("remote", bReq) |
| 70 | require.Nil(t, err) |
| 71 | assert.Equal(t, "basic", bRes.TransferAdapterName) |
| 72 | if assert.Equal(t, 1, len(bRes.Objects)) { |
| 73 | assert.Equal(t, "a", bRes.Objects[0].Oid) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestAPIBatchOnlyBasic(t *testing.T) { |
| 78 | require.NotNil(t, batchReqSchema, batchReqSchema.Source) |
| 79 | require.NotNil(t, batchResSchema, batchResSchema.Source) |
| 80 | |
| 81 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 82 | if r.URL.Path != "/api/objects/batch" { |
| 83 | w.WriteHeader(404) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | assert.Equal(t, "POST", r.Method) |
| 88 | |
| 89 | bodyLoader, body := gojsonschema.NewReaderLoader(r.Body) |
| 90 | bReq := &batchRequest{} |
| 91 | err := json.NewDecoder(body).Decode(bReq) |
| 92 | r.Body.Close() |
| 93 | assert.Nil(t, err) |
| 94 | assertSchema(t, batchReqSchema, bodyLoader) |
| 95 | |
| 96 | assert.Equal(t, 0, len(bReq.TransferAdapterNames)) |
| 97 | if assert.Equal(t, 1, len(bReq.Objects)) { |
| 98 | assert.Equal(t, "a", bReq.Objects[0].Oid) |
| 99 | } |
| 100 | |
| 101 | w.Header().Set("Content-Type", "application/json") |
| 102 | writeLoader, resWriter := gojsonschema.NewWriterLoader(w) |
| 103 | err = json.NewEncoder(resWriter).Encode(&BatchResponse{ |
| 104 | TransferAdapterName: "basic", |
| 105 | Objects: make([]*Transfer, 0), |
| 106 | }) |
| 107 | |
| 108 | assert.Nil(t, err) |
| 109 | assertSchema(t, batchResSchema, writeLoader) |
| 110 | })) |
| 111 | defer srv.Close() |
| 112 | |
| 113 | c := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{ |
| 114 | "lfs.url": srv.URL + "/api", |
| 115 | })) |
| 116 | |
| 117 | tqc := &tqClient{Client: c} |
| 118 | bReq := &batchRequest{ |
| 119 | TransferAdapterNames: []string{"basic"}, |
| 120 | Objects: []*Transfer{ |
| 121 | &Transfer{Oid: "a", Size: 1}, |
| 122 | }, |
| 123 | } |
| 124 | bRes, err := tqc.Batch("remote", bReq) |
| 125 | require.Nil(t, err) |
| 126 | assert.Equal(t, "basic", bRes.TransferAdapterName) |
| 127 | } |
| 128 | |
| 129 | func TestAPIBatchEmptyObjects(t *testing.T) { |
| 130 | c := lfsapi.NewClient(nil) |
| 131 | |
| 132 | tqc := &tqClient{Client: c} |
| 133 | bReq := &batchRequest{ |
| 134 | TransferAdapterNames: []string{"basic", "whatev"}, |
| 135 | } |
| 136 | bRes, err := tqc.Batch("remote", bReq) |
| 137 | require.Nil(t, err) |
| 138 | assert.Equal(t, "", bRes.TransferAdapterName) |
| 139 | assert.Equal(t, 0, len(bRes.Objects)) |
| 140 | } |
| 141 | |
| 142 | var ( |
| 143 | batchReqSchema *sourcedSchema |
| 144 | batchResSchema *sourcedSchema |
| 145 | ) |
| 146 | |
| 147 | func init() { |
| 148 | wd, err := os.Getwd() |
| 149 | if err != nil { |
| 150 | fmt.Println("getwd error:", err) |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | batchReqSchema = getSchema(wd, "schemas/http-batch-request-schema.json") |
| 155 | batchResSchema = getSchema(wd, "schemas/http-batch-response-schema.json") |
| 156 | } |
| 157 | |
| 158 | type sourcedSchema struct { |
| 159 | Source string |
| 160 | *gojsonschema.Schema |
| 161 | } |
| 162 | |
| 163 | func getSchema(wd, relpath string) *sourcedSchema { |
| 164 | abspath := filepath.ToSlash(filepath.Join(wd, relpath)) |
| 165 | s, err := gojsonschema.NewSchema(gojsonschema.NewReferenceLoader(fmt.Sprintf("file:///%s", abspath))) |
| 166 | if err != nil { |
| 167 | fmt.Printf("schema load error for %q: %+v\n", relpath, err) |
| 168 | } |
| 169 | return &sourcedSchema{Source: relpath, Schema: s} |
| 170 | } |
| 171 | |
| 172 | func assertSchema(t *testing.T, schema *sourcedSchema, dataLoader gojsonschema.JSONLoader) { |
| 173 | res, err := schema.Validate(dataLoader) |
| 174 | if assert.Nil(t, err) { |
| 175 | if res.Valid() { |
| 176 | return |
| 177 | } |
| 178 | |
| 179 | resErrors := res.Errors() |
| 180 | valErrors := make([]string, 0, len(resErrors)) |
| 181 | for _, resErr := range resErrors { |
| 182 | valErrors = append(valErrors, resErr.String()) |
| 183 | } |
| 184 | t.Errorf("Schema: %s\n%s", schema.Source, strings.Join(valErrors, "\n")) |
| 185 | } |
| 186 | } |