|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + problem "github.com/developer-overheid-nl/don-api-register/pkg/api_client/helpers/problem" |
| 10 | + "github.com/developer-overheid-nl/don-api-register/pkg/api_client/models" |
| 11 | + "github.com/developer-overheid-nl/don-api-register/pkg/api_client/services" |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAcceptsJsonLd(t *testing.T) { |
| 18 | + t.Run("accepts explicit jsonld", func(t *testing.T) { |
| 19 | + assert.True(t, AcceptsJsonLd("application/ld+json")) |
| 20 | + }) |
| 21 | + |
| 22 | + t.Run("accepts jsonld between other media types", func(t *testing.T) { |
| 23 | + assert.True(t, AcceptsJsonLd("application/json, application/ld+json; q=0.9")) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("rejects plain json", func(t *testing.T) { |
| 27 | + assert.False(t, AcceptsJsonLd("application/json")) |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("rejects wildcard only", func(t *testing.T) { |
| 31 | + assert.False(t, AcceptsJsonLd("*/*")) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func TestRetrieveApiJsonLd_Handler(t *testing.T) { |
| 36 | + repo := &stubRepo{ |
| 37 | + retrFunc: func(ctx context.Context, id string) (*models.Api, error) { |
| 38 | + return &models.Api{ |
| 39 | + Id: id, |
| 40 | + Title: "JSON-LD API", |
| 41 | + Description: "Beschrijving", |
| 42 | + OasUri: "https://example.com/openapi.json", |
| 43 | + ContactName: "API Team", |
| 44 | + ContactEmail: "team@example.com", |
| 45 | + ContactUrl: "https://example.com/contact", |
| 46 | + OAS: models.OASMetadata{Version: "3.1.0"}, |
| 47 | + Organisation: &models.Organisation{ |
| 48 | + Uri: "https://example.com/orgs/1", |
| 49 | + Label: "Org 1", |
| 50 | + }, |
| 51 | + }, nil |
| 52 | + }, |
| 53 | + lintResFunc: func(ctx context.Context, apiID string) ([]models.LintResult, error) { |
| 54 | + return nil, nil |
| 55 | + }, |
| 56 | + } |
| 57 | + svc := services.NewAPIsAPIService(repo) |
| 58 | + ctrl := NewAPIsAPIController(svc) |
| 59 | + |
| 60 | + w := httptest.NewRecorder() |
| 61 | + ctx, _ := gin.CreateTestContext(w) |
| 62 | + req := httptest.NewRequest("GET", "/v1/apis/api-1", nil) |
| 63 | + req.Header.Set("Accept", "application/ld+json") |
| 64 | + ctx.Request = req |
| 65 | + |
| 66 | + err := ctrl.RetrieveApiJsonLd(ctx, &models.ApiParams{Id: "api-1"}) |
| 67 | + require.NoError(t, err) |
| 68 | + assert.Equal(t, 200, w.Code) |
| 69 | + assert.Contains(t, w.Header().Get("Content-Type"), "application/ld+json") |
| 70 | + |
| 71 | + var body models.ApiDetailJsonLd |
| 72 | + require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body)) |
| 73 | + assert.Equal(t, "dcat:DataService", body.Type) |
| 74 | + assert.Equal(t, "api-1", body.Identifier) |
| 75 | + assert.Equal(t, "JSON-LD API", body.Title) |
| 76 | + assert.Equal(t, "Beschrijving", body.Description) |
| 77 | + assert.Equal(t, "https://example.com/openapi.json", body.EndpointDescription) |
| 78 | + assert.Equal(t, "API Team", body.ContactPoint.FN) |
| 79 | + assert.Equal(t, "mailto:team@example.com", body.ContactPoint.HasEmail) |
| 80 | + assert.Equal(t, "https://example.com/contact", body.ContactPoint.HasURL) |
| 81 | + assert.Equal(t, "https://example.com/orgs/1", body.Publisher) |
| 82 | + assert.Equal(t, []string{"https://spec.openapis.org/oas/v3.1.0.html"}, body.ConformsTo) |
| 83 | +} |
| 84 | + |
| 85 | +func TestRetrieveApiJsonLd_NotFound(t *testing.T) { |
| 86 | + repo := &stubRepo{ |
| 87 | + retrFunc: func(ctx context.Context, id string) (*models.Api, error) { |
| 88 | + return nil, nil |
| 89 | + }, |
| 90 | + } |
| 91 | + svc := services.NewAPIsAPIService(repo) |
| 92 | + ctrl := NewAPIsAPIController(svc) |
| 93 | + |
| 94 | + w := httptest.NewRecorder() |
| 95 | + ctx, _ := gin.CreateTestContext(w) |
| 96 | + req := httptest.NewRequest("GET", "/v1/apis/missing", nil) |
| 97 | + req.Header.Set("Accept", "application/ld+json") |
| 98 | + ctx.Request = req |
| 99 | + |
| 100 | + err := ctrl.RetrieveApiJsonLd(ctx, &models.ApiParams{Id: "missing"}) |
| 101 | + apiErr, ok := err.(problem.APIError) |
| 102 | + require.True(t, ok) |
| 103 | + assert.Equal(t, 404, apiErr.Status) |
| 104 | + if assert.Len(t, apiErr.Errors, 1) { |
| 105 | + assert.Contains(t, apiErr.Errors[0].Detail, "Api not found") |
| 106 | + } |
| 107 | +} |
0 commit comments