github.com/thanos-io/thanos@v0.32.5/pkg/api/api_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 // Copyright 2016 The Prometheus Authors 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package api 18 19 import ( 20 "encoding/json" 21 "io" 22 "net/http" 23 "net/http/httptest" 24 "reflect" 25 "testing" 26 27 "github.com/go-kit/log" 28 "github.com/opentracing/opentracing-go" 29 "github.com/pkg/errors" 30 "github.com/prometheus/common/route" 31 32 "github.com/efficientgo/core/testutil" 33 extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http" 34 "github.com/thanos-io/thanos/pkg/logging" 35 ) 36 37 func TestRespondSuccess(t *testing.T) { 38 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 39 Respond(w, "test", nil) 40 })) 41 defer s.Close() 42 43 resp, err := http.Get(s.URL) 44 if err != nil { 45 t.Fatalf("Error on test request: %s", err) 46 } 47 body, err := io.ReadAll(resp.Body) 48 defer func() { testutil.Ok(t, resp.Body.Close()) }() 49 if err != nil { 50 t.Fatalf("Error reading response body: %s", err) 51 } 52 53 if resp.StatusCode != 200 { 54 t.Fatalf("Return code %d expected in success response but got %d", 200, resp.StatusCode) 55 } 56 if h := resp.Header.Get("Content-Type"); h != "application/json" { 57 t.Fatalf("Expected Content-Type %q but got %q", "application/json", h) 58 } 59 60 var res response 61 if err = json.Unmarshal([]byte(body), &res); err != nil { 62 t.Fatalf("Error unmarshaling JSON body: %s", err) 63 } 64 65 exp := &response{ 66 Status: StatusSuccess, 67 Data: "test", 68 } 69 if !reflect.DeepEqual(&res, exp) { 70 t.Fatalf("Expected response \n%v\n but got \n%v\n", res, exp) 71 } 72 } 73 74 func TestRespondError(t *testing.T) { 75 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 76 RespondError(w, &ApiError{ErrorTimeout, errors.New("message")}, "test") 77 })) 78 defer s.Close() 79 80 resp, err := http.Get(s.URL) 81 if err != nil { 82 t.Fatalf("Error on test request: %s", err) 83 } 84 body, err := io.ReadAll(resp.Body) 85 defer func() { testutil.Ok(t, resp.Body.Close()) }() 86 if err != nil { 87 t.Fatalf("Error reading response body: %s", err) 88 } 89 90 if want, have := http.StatusServiceUnavailable, resp.StatusCode; want != have { 91 t.Fatalf("Return code %d expected in error response but got %d", want, have) 92 } 93 if h := resp.Header.Get("Content-Type"); h != "application/json" { 94 t.Fatalf("Expected Content-Type %q but got %q", "application/json", h) 95 } 96 97 var res response 98 if err = json.Unmarshal([]byte(body), &res); err != nil { 99 t.Fatalf("Error unmarshaling JSON body: %s", err) 100 } 101 102 exp := &response{ 103 Status: StatusError, 104 Data: "test", 105 ErrorType: ErrorTimeout, 106 Error: "message", 107 } 108 if !reflect.DeepEqual(&res, exp) { 109 t.Fatalf("Expected response \n%v\n but got \n%v\n", res, exp) 110 } 111 } 112 113 func TestOptionsMethod(t *testing.T) { 114 r := route.New() 115 api := &BaseAPI{} 116 logMiddleware := logging.NewHTTPServerMiddleware(log.NewNopLogger()) 117 api.Register(r, &opentracing.NoopTracer{}, log.NewNopLogger(), extpromhttp.NewNopInstrumentationMiddleware(), logMiddleware) 118 119 s := httptest.NewServer(r) 120 defer s.Close() 121 122 req, err := http.NewRequest("OPTIONS", s.URL+"/any_path", nil) 123 if err != nil { 124 t.Fatalf("Error creating OPTIONS request: %s", err) 125 } 126 client := &http.Client{} 127 resp, err := client.Do(req) 128 if err != nil { 129 t.Fatalf("Error executing OPTIONS request: %s", err) 130 } 131 132 if resp.StatusCode != http.StatusNoContent { 133 t.Fatalf("Expected status %d, got %d", http.StatusNoContent, resp.StatusCode) 134 } 135 136 for h, v := range corsHeaders { 137 if resp.Header.Get(h) != v { 138 t.Fatalf("Expected %q for header %q, got %q", v, h, resp.Header.Get(h)) 139 } 140 } 141 }