|
| 1 | +package eywa_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/imperfect-fourth/eywa" |
| 12 | +) |
| 13 | + |
| 14 | +type MockQuerable struct { |
| 15 | + QueryStr string `json:"query"` |
| 16 | + Vars map[string]interface{} `json:"variables"` |
| 17 | +} |
| 18 | + |
| 19 | +func (m *MockQuerable) Query() string { |
| 20 | + return m.QueryStr |
| 21 | +} |
| 22 | + |
| 23 | +func (m *MockQuerable) Variables() map[string]interface{} { |
| 24 | + return m.Vars |
| 25 | +} |
| 26 | + |
| 27 | +func TestRawClient(t *testing.T) { |
| 28 | + tt := []struct { |
| 29 | + name string |
| 30 | + server *httptest.Server |
| 31 | + expectedErr error |
| 32 | + expectedResponse []byte |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "Valid GQL response", |
| 36 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 37 | + w.WriteHeader(http.StatusOK) |
| 38 | + w.Write([]byte(`{"data": {"test": "test"}}`)) |
| 39 | + })), |
| 40 | + expectedResponse: []byte(`{"data": {"test": "test"}}`), |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "A 401 response", |
| 44 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | + w.WriteHeader(http.StatusUnauthorized) |
| 46 | + w.Write([]byte(`{"error": {"message": "unauthorized"}}`)) |
| 47 | + })), |
| 48 | + expectedResponse: []byte(`{"error": {"message": "unauthorized"}}`), |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "A 403 response", |
| 52 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 53 | + w.WriteHeader(http.StatusUnauthorized) |
| 54 | + w.Write([]byte(`{"error": {"message": "forbidden"}}`)) |
| 55 | + })), |
| 56 | + expectedResponse: []byte(`{"error": {"message": "forbidden"}}`), |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "A 503 non-json response", |
| 60 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 61 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 62 | + w.Write([]byte(`Service unavailable`)) |
| 63 | + })), |
| 64 | + expectedResponse: []byte(`Service unavailable`), |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range tt { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + gqlClient := eywa.NewClient(tc.server.URL, nil) |
| 71 | + resp, err := gqlClient.Raw(context.TODO(), &MockQuerable{}) |
| 72 | + if err != nil && tc.expectedErr == nil { |
| 73 | + t.Errorf("Expected no error, got %v", err) |
| 74 | + return |
| 75 | + } |
| 76 | + if err == nil && tc.expectedErr != nil { |
| 77 | + t.Errorf("Expected error %v, got nil", tc.expectedErr) |
| 78 | + return |
| 79 | + } |
| 80 | + defer resp.Body.Close() |
| 81 | + |
| 82 | + respBody, err := io.ReadAll(resp.Body) |
| 83 | + if err != nil { |
| 84 | + t.Errorf("Error reading response body: %v", err) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if !bytes.Equal(tc.expectedResponse, respBody) { |
| 89 | + t.Errorf("Expected response %s, got %s", string(tc.expectedResponse), string(respBody)) |
| 90 | + return |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments