|
| 1 | +package upload |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestClient_UploadViaReader_Success(t *testing.T) { |
| 15 | + // Create a test server for the upload endpoint |
| 16 | + uploadServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + if r.Method != "PUT" { |
| 18 | + t.Errorf("Expected PUT method, got %s", r.Method) |
| 19 | + } |
| 20 | + |
| 21 | + // Verify content type |
| 22 | + expectedContentType := "application/json" |
| 23 | + if r.Header.Get("Content-Type") != expectedContentType { |
| 24 | + t.Errorf("Expected Content-Type %s, got %s", expectedContentType, r.Header.Get("Content-Type")) |
| 25 | + } |
| 26 | + |
| 27 | + // Read and verify content |
| 28 | + body, err := io.ReadAll(r.Body) |
| 29 | + if err != nil { |
| 30 | + t.Errorf("Failed to read request body: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + expectedContent := `{"test": "data"}` |
| 34 | + if string(body) != expectedContent { |
| 35 | + t.Errorf("Expected content %s, got %s", expectedContent, string(body)) |
| 36 | + } |
| 37 | + |
| 38 | + w.WriteHeader(http.StatusOK) |
| 39 | + w.Write([]byte("Upload successful")) |
| 40 | + })) |
| 41 | + defer uploadServer.Close() |
| 42 | + |
| 43 | + // Create a test server for the GraphQL endpoint |
| 44 | + graphqlServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | + if r.Method != "POST" { |
| 46 | + t.Errorf("Expected POST method for GraphQL, got %s", r.Method) |
| 47 | + } |
| 48 | + |
| 49 | + // Return a mock presigned URL response |
| 50 | + response := `{ |
| 51 | + "data": { |
| 52 | + "storage_presigned_upload_url": { |
| 53 | + "url": "` + uploadServer.URL + `", |
| 54 | + "expired_at": "` + time.Now().Add(time.Hour).Format(time.RFC3339) + `" |
| 55 | + } |
| 56 | + } |
| 57 | + }` |
| 58 | + |
| 59 | + w.Header().Set("Content-Type", "application/json") |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + w.Write([]byte(response)) |
| 62 | + })) |
| 63 | + defer graphqlServer.Close() |
| 64 | + |
| 65 | + // Create client with test servers |
| 66 | + client := NewClient(graphqlServer.URL, "test-api-key") |
| 67 | + |
| 68 | + // Test content |
| 69 | + testContent := `{"test": "data"}` |
| 70 | + reader := strings.NewReader(testContent) |
| 71 | + |
| 72 | + // Call the function under test |
| 73 | + err := client.UploadViaReader(context.Background(), reader, "application/json", "test/file.json") |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + t.Errorf("Expected no error, but got: %v", err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestClient_UploadViaReader_ReaderError(t *testing.T) { |
| 81 | + // Test case where the reader itself fails |
| 82 | + client := NewClient("https://test.example.com/graphql", "test-key") |
| 83 | + |
| 84 | + // Create a reader that will fail |
| 85 | + failingReader := &failingReader{err: errors.New("reader error")} |
| 86 | + |
| 87 | + err := client.UploadViaReader(context.Background(), failingReader, "text/plain", "test/file.txt") |
| 88 | + |
| 89 | + expectedError := "failed to read content: reader error" |
| 90 | + if err == nil { |
| 91 | + t.Errorf("Expected error containing %q, but got no error", expectedError) |
| 92 | + } else if !strings.Contains(err.Error(), expectedError) { |
| 93 | + t.Errorf("Expected error containing %q, but got: %v", expectedError, err) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// failingReader is a mock reader that always returns an error |
| 98 | +type failingReader struct { |
| 99 | + err error |
| 100 | +} |
| 101 | + |
| 102 | +func (f *failingReader) Read(p []byte) (n int, err error) { |
| 103 | + return 0, f.err |
| 104 | +} |
| 105 | + |
| 106 | +func TestClient_UploadViaReader_GraphQLError(t *testing.T) { |
| 107 | + // Create a test server that returns GraphQL error |
| 108 | + graphqlServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 109 | + w.Header().Set("Content-Type", "application/json") |
| 110 | + w.WriteHeader(http.StatusBadRequest) |
| 111 | + w.Write([]byte(`{"errors": [{"message": "Invalid query"}]}`)) |
| 112 | + })) |
| 113 | + defer graphqlServer.Close() |
| 114 | + |
| 115 | + client := NewClient(graphqlServer.URL, "test-key") |
| 116 | + reader := strings.NewReader("test content") |
| 117 | + |
| 118 | + err := client.UploadViaReader(context.Background(), reader, "text/plain", "test/file.txt") |
| 119 | + |
| 120 | + if err == nil { |
| 121 | + t.Error("Expected error due to GraphQL failure, but got no error") |
| 122 | + } else if !strings.Contains(err.Error(), "failed to get presigned upload URL") { |
| 123 | + t.Errorf("Expected GraphQL error, but got: %v", err) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestClient_UploadViaReader_UploadError(t *testing.T) { |
| 128 | + // Create upload server that returns error |
| 129 | + uploadServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 130 | + w.WriteHeader(http.StatusForbidden) |
| 131 | + w.Write([]byte("Access denied")) |
| 132 | + })) |
| 133 | + defer uploadServer.Close() |
| 134 | + |
| 135 | + // Create GraphQL server that returns the upload server URL |
| 136 | + graphqlServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 137 | + response := `{ |
| 138 | + "data": { |
| 139 | + "storage_presigned_upload_url": { |
| 140 | + "url": "` + uploadServer.URL + `", |
| 141 | + "expired_at": "` + time.Now().Add(time.Hour).Format(time.RFC3339) + `" |
| 142 | + } |
| 143 | + } |
| 144 | + }` |
| 145 | + w.Header().Set("Content-Type", "application/json") |
| 146 | + w.WriteHeader(http.StatusOK) |
| 147 | + w.Write([]byte(response)) |
| 148 | + })) |
| 149 | + defer graphqlServer.Close() |
| 150 | + |
| 151 | + client := NewClient(graphqlServer.URL, "test-key") |
| 152 | + reader := strings.NewReader("test content") |
| 153 | + |
| 154 | + err := client.UploadViaReader(context.Background(), reader, "text/plain", "test/file.txt") |
| 155 | + if err == nil { |
| 156 | + t.Error("Expected upload error, but got no error") |
| 157 | + } else if !strings.Contains(err.Error(), "upload failed with status 403") { |
| 158 | + t.Errorf("Expected upload status error, but got: %v", err) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestClient_UploadViaReader_EmptyContent(t *testing.T) { |
| 163 | + // Test with empty content |
| 164 | + uploadServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 165 | + body, _ := io.ReadAll(r.Body) |
| 166 | + if len(body) != 0 { |
| 167 | + t.Errorf("Expected empty body, got %d bytes", len(body)) |
| 168 | + } |
| 169 | + w.WriteHeader(http.StatusOK) |
| 170 | + })) |
| 171 | + defer uploadServer.Close() |
| 172 | + |
| 173 | + graphqlServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 174 | + response := `{ |
| 175 | + "data": { |
| 176 | + "storage_presigned_upload_url": { |
| 177 | + "url": "` + uploadServer.URL + `", |
| 178 | + "expired_at": "` + time.Now().Add(time.Hour).Format(time.RFC3339) + `" |
| 179 | + } |
| 180 | + } |
| 181 | + }` |
| 182 | + w.Header().Set("Content-Type", "application/json") |
| 183 | + w.WriteHeader(http.StatusOK) |
| 184 | + w.Write([]byte(response)) |
| 185 | + })) |
| 186 | + defer graphqlServer.Close() |
| 187 | + |
| 188 | + client := NewClient(graphqlServer.URL, "test-key") |
| 189 | + reader := strings.NewReader("") |
| 190 | + |
| 191 | + err := client.UploadViaReader(context.Background(), reader, "text/plain", "test/empty.txt") |
| 192 | + |
| 193 | + if err != nil { |
| 194 | + t.Errorf("Expected no error for empty content, but got: %v", err) |
| 195 | + } |
| 196 | +} |
0 commit comments