github.com/avenga/couper@v1.12.2/server/http_definitions_test.go (about) 1 package server_test 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "path/filepath" 10 "testing" 11 "time" 12 13 "github.com/google/go-cmp/cmp" 14 "github.com/sirupsen/logrus" 15 16 "github.com/avenga/couper/internal/test" 17 ) 18 19 // TODO: relocate while refactoring integration tests 20 21 func TestDefinitions_Jobs(t *testing.T) { 22 type testcase struct { 23 name string 24 fileName string 25 origin http.Handler 26 wantErr bool 27 wantFields logrus.Fields 28 wantLevel logrus.Level 29 wantMsg string 30 } 31 32 const basePath = "testdata/definitions" 33 34 for _, tc := range []testcase{ 35 {"without label", "01_job.hcl", http.HandlerFunc(nil), true, nil, logrus.InfoLevel, ""}, // wantLevel not used 36 {"without interval", "02_job.hcl", http.HandlerFunc(nil), true, nil, logrus.InfoLevel, ""}, // wantLevel not used 37 {"with negative interval", "03_job.hcl", http.HandlerFunc(nil), true, nil, logrus.InfoLevel, ""}, // wantLevel not used 38 {"variable reference", "04_job.hcl", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 39 payload := map[string]string{ 40 "prop1": "val1", 41 "prop2": "val2", 42 } 43 b, _ := json.Marshal(payload) 44 45 switch req.Method { 46 case http.MethodGet: 47 w.Header().Set("Content-Type", "application/json") 48 w.Write(b) 49 case http.MethodPost: 50 r, _ := io.ReadAll(req.Body) 51 if !bytes.Equal(b, r) { 52 w.WriteHeader(http.StatusInternalServerError) 53 return 54 } 55 w.WriteHeader(http.StatusOK) 56 } 57 }), false, logrus.Fields{"custom": logrus.Fields{ 58 "status_a": float64(http.StatusOK), 59 "status_b": float64(http.StatusOK), 60 }}, logrus.InfoLevel, ""}, 61 {"unexpected status", "05_job.hcl", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 62 w.WriteHeader(http.StatusOK) 63 }), false, logrus.Fields{"error_type": "unexpected_status"}, logrus.ErrorLevel, "endpoint error"}, 64 } { 65 t.Run(tc.name, func(st *testing.T) { 66 origin := httptest.NewServer(tc.origin) 67 defer origin.Close() 68 69 helper := test.New(st) 70 71 shutdown, hook, err := newCouperWithTemplate(filepath.Join(basePath, tc.fileName), helper, map[string]interface{}{ 72 "origin": origin.URL, 73 }) 74 75 if (err != nil) != tc.wantErr { 76 st.Fatalf("want error: %v, got: %v", tc.wantErr, err) 77 } else if tc.wantErr && err != nil { 78 return 79 } 80 81 defer shutdown() 82 83 time.Sleep(time.Second / 4) 84 85 for _, entry := range hook.AllEntries() { 86 if entry.Data["type"] == "couper_job" { 87 for k := range tc.wantFields { 88 if diff := cmp.Diff(entry.Data[k], tc.wantFields[k]); diff != "" { 89 st.Errorf("expected log fields %q:\n%v", k, diff) 90 } 91 } 92 if entry.Level != tc.wantLevel { 93 st.Errorf("want level: %d, got: %d", tc.wantLevel, entry.Level) 94 } 95 if entry.Message != tc.wantMsg { 96 st.Errorf("want message: %q, got: %q", tc.wantMsg, entry.Message) 97 } 98 continue 99 } 100 if entry.Data["status"] != 200 { 101 st.Errorf("expected status OK, got: %v", entry.Data["status"]) 102 } 103 } 104 105 }) 106 } 107 }