|
| 1 | +package eywa_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/imperfect-fourth/eywa" |
| 13 | +) |
| 14 | + |
| 15 | +type MockQuerable struct { |
| 16 | + QueryStr string `json:"query"` |
| 17 | + Vars map[string]interface{} `json:"variables"` |
| 18 | +} |
| 19 | + |
| 20 | +func (m *MockQuerable) Query() string { |
| 21 | + return m.QueryStr |
| 22 | +} |
| 23 | + |
| 24 | +func (m *MockQuerable) Variables() map[string]interface{} { |
| 25 | + return m.Vars |
| 26 | +} |
| 27 | + |
| 28 | +func TestDoClient(t *testing.T) { |
| 29 | + tt := []struct { |
| 30 | + name string |
| 31 | + server *httptest.Server |
| 32 | + expectedErr error |
| 33 | + expectedResponse []byte |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "Valid GQL response", |
| 37 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 38 | + w.WriteHeader(http.StatusOK) |
| 39 | + w.Write([]byte(`{"data": {"test": "test"}}`)) |
| 40 | + })), |
| 41 | + expectedResponse: []byte(`{"data": {"test": "test"}}`), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "A 401 response", |
| 45 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 46 | + w.WriteHeader(http.StatusUnauthorized) |
| 47 | + w.Write([]byte(`{"error": {"message": "unauthorized"}}`)) |
| 48 | + })), |
| 49 | + expectedErr: eywa.ErrHTTPRequestFailed, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "A 401 status with no response", |
| 53 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 54 | + w.WriteHeader(http.StatusUnauthorized) |
| 55 | + })), |
| 56 | + expectedErr: eywa.ErrHTTPRequestFailed, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "A 403 response", |
| 60 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 61 | + w.WriteHeader(http.StatusUnauthorized) |
| 62 | + w.Write([]byte(`{"error": {"message": "forbidden"}}`)) |
| 63 | + })), |
| 64 | + expectedErr: eywa.ErrHTTPRequestFailed, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "A 503 non-json response", |
| 68 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 69 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 70 | + w.Write([]byte(`Service unavailable`)) |
| 71 | + })), |
| 72 | + expectedErr: eywa.ErrHTTPRequestFailed, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "A 301 Redirect", |
| 76 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | + w.WriteHeader(http.StatusMovedPermanently) |
| 78 | + w.Write([]byte(`Moved Permanently`)) |
| 79 | + })), |
| 80 | + expectedErr: eywa.ErrHTTPRequestRedirect, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range tt { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + gqlClient := eywa.NewClient(tc.server.URL, nil) |
| 87 | + resp, err := gqlClient.Do(context.TODO(), &MockQuerable{}) |
| 88 | + if err != nil && tc.expectedErr == nil { |
| 89 | + t.Errorf("Expected no error, got %v", err) |
| 90 | + return |
| 91 | + } |
| 92 | + if err == nil && tc.expectedErr != nil { |
| 93 | + t.Errorf("Expected error %v, got nil", tc.expectedErr) |
| 94 | + return |
| 95 | + } |
| 96 | + if err != nil && tc.expectedErr != nil { |
| 97 | + if !errors.Is(err, tc.expectedErr) { |
| 98 | + t.Errorf("Expected error %v, got %v", tc.expectedErr, err) |
| 99 | + return |
| 100 | + } |
| 101 | + return |
| 102 | + } |
| 103 | + if !bytes.Equal(tc.expectedResponse, resp.Bytes()) { |
| 104 | + t.Errorf("Expected response %s, got %s", string(tc.expectedResponse), resp.String()) |
| 105 | + return |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestRawClient(t *testing.T) { |
| 112 | + tt := []struct { |
| 113 | + name string |
| 114 | + server *httptest.Server |
| 115 | + expectedErr error |
| 116 | + expectedResponse []byte |
| 117 | + }{ |
| 118 | + { |
| 119 | + name: "Valid GQL response", |
| 120 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 121 | + w.WriteHeader(http.StatusOK) |
| 122 | + w.Write([]byte(`{"data": {"test": "test"}}`)) |
| 123 | + })), |
| 124 | + expectedResponse: []byte(`{"data": {"test": "test"}}`), |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "A 401 response", |
| 128 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 129 | + w.WriteHeader(http.StatusUnauthorized) |
| 130 | + w.Write([]byte(`{"error": {"message": "unauthorized"}}`)) |
| 131 | + })), |
| 132 | + expectedResponse: []byte(`{"error": {"message": "unauthorized"}}`), |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "A 403 response", |
| 136 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 137 | + w.WriteHeader(http.StatusUnauthorized) |
| 138 | + w.Write([]byte(`{"error": {"message": "forbidden"}}`)) |
| 139 | + })), |
| 140 | + expectedResponse: []byte(`{"error": {"message": "forbidden"}}`), |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "A 503 non-json response", |
| 144 | + server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 145 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 146 | + w.Write([]byte(`Service unavailable`)) |
| 147 | + })), |
| 148 | + expectedResponse: []byte(`Service unavailable`), |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + for _, tc := range tt { |
| 153 | + t.Run(tc.name, func(t *testing.T) { |
| 154 | + gqlClient := eywa.NewClient(tc.server.URL, nil) |
| 155 | + resp, err := gqlClient.Raw(context.TODO(), &MockQuerable{}) |
| 156 | + if err != nil && tc.expectedErr == nil { |
| 157 | + t.Errorf("Expected no error, got %v", err) |
| 158 | + return |
| 159 | + } |
| 160 | + if err == nil && tc.expectedErr != nil { |
| 161 | + t.Errorf("Expected error %v, got nil", tc.expectedErr) |
| 162 | + return |
| 163 | + } |
| 164 | + defer resp.Body.Close() |
| 165 | + |
| 166 | + respBody, err := io.ReadAll(resp.Body) |
| 167 | + if err != nil { |
| 168 | + t.Errorf("Error reading response body: %v", err) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + if !bytes.Equal(tc.expectedResponse, respBody) { |
| 173 | + t.Errorf("Expected response %s, got %s", string(tc.expectedResponse), string(respBody)) |
| 174 | + return |
| 175 | + } |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
0 commit comments