|
| 1 | +package unstructured |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGetDestination(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + client, mux := testclient(t) |
| 15 | + |
| 16 | + id := "0c363dec-3c70-45ee-8041-481044a6e1cc" |
| 17 | + mux.GetDestination = func(w http.ResponseWriter, r *http.Request) { |
| 18 | + if val := r.PathValue("id"); val != id { |
| 19 | + http.Error(w, "destination ID "+val+" not found", http.StatusNotFound) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + response := []byte(`{` + |
| 24 | + ` "config": {` + |
| 25 | + ` "remote_url": "s3://mock-s3-connector",` + |
| 26 | + ` "anonymous": false,` + |
| 27 | + ` "key": "**********",` + |
| 28 | + ` "secret": "**********",` + |
| 29 | + ` "token": null,` + |
| 30 | + ` "endpoint_url": null` + |
| 31 | + ` },` + |
| 32 | + ` "created_at": "2025-08-22T08:47:29.802Z",` + |
| 33 | + ` "id": "` + id + `",` + |
| 34 | + ` "name": "test_destination_name",` + |
| 35 | + ` "type": "s3"` + |
| 36 | + `}`) |
| 37 | + |
| 38 | + w.Header().Set("Content-Type", "application/json") |
| 39 | + w.Header().Set("Content-Length", strconv.Itoa(len(response))) |
| 40 | + w.Write(response) |
| 41 | + } |
| 42 | + |
| 43 | + destination, err := client.GetDestination(t.Context(), id) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("failed to get destination: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + if err := errors.Join( |
| 49 | + eq("destination.id", destination.ID, id), |
| 50 | + eq("destination.name", destination.Name, "test_destination_name"), |
| 51 | + eq("destination.type", destination.Type, "s3"), |
| 52 | + equal("destination.created_at", destination.CreatedAt, time.Date(2025, 8, 22, 8, 47, 29, 802000000, time.UTC)), |
| 53 | + ); err != nil { |
| 54 | + t.Error(err) |
| 55 | + } |
| 56 | + |
| 57 | + cfg, ok := destination.Config.(*S3DestinationConnectorConfig) |
| 58 | + if !ok { |
| 59 | + t.Errorf("expected destination config to be %T, got %T", cfg, destination.Config) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestGetDestinationNotFound(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + client, mux := testclient(t) |
| 67 | + |
| 68 | + id := "0c363dec-3c70-45ee-8041-481044a6e1cc" |
| 69 | + mux.GetDestination = func(w http.ResponseWriter, r *http.Request) { |
| 70 | + http.Error(w, "destination ID "+r.PathValue("id")+" not found", http.StatusNotFound) |
| 71 | + } |
| 72 | + |
| 73 | + _, err := client.GetDestination(t.Context(), id) |
| 74 | + if err == nil { |
| 75 | + t.Fatalf("expected error, got nil") |
| 76 | + } |
| 77 | + |
| 78 | + var apierr *APIError |
| 79 | + if !errors.As(err, &apierr) { |
| 80 | + t.Fatalf("expected error to be an %T, got %T", apierr, err) |
| 81 | + } |
| 82 | + |
| 83 | + if apierr.Code != http.StatusNotFound { |
| 84 | + t.Fatalf("expected error code to be %d, got %d", http.StatusNotFound, apierr.Code) |
| 85 | + } |
| 86 | +} |
0 commit comments