diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f9ea54ec..b824d7152 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bugfixes * Update Go runtime to 1.20.4. [#987](https://github.com/elastic/package-registry/pull/987) [#1002](https://github.com/elastic/package-registry/pull/1002) +* Add fields related to subcategories into categories entrypoint with proxy mode [#1004](https://github.com/elastic/package-registry/pull/1004) ### Added diff --git a/categories.go b/categories.go index a5db1e7ab..39e160a1b 100644 --- a/categories.go +++ b/categories.go @@ -72,9 +72,11 @@ func categoriesHandlerWithProxyMode(logger *zap.Logger, indexer Indexer, proxyMo for _, category := range proxiedCategories { if _, ok := categories[category.Id]; !ok { categories[category.Id] = &packages.Category{ - Id: category.Id, - Title: category.Title, - Count: category.Count, + Id: category.Id, + Title: category.Title, + Count: category.Count, + ParentId: category.ParentId, + ParentTitle: category.ParentTitle, } } else { categories[category.Id].Count += category.Count diff --git a/proxy_mode_test.go b/proxy_mode_test.go new file mode 100644 index 000000000..7c03d7dd7 --- /dev/null +++ b/proxy_mode_test.go @@ -0,0 +1,71 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package main + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/elastic/package-registry/packages" + "github.com/elastic/package-registry/proxymode" +) + +func TestCategoriesWithProxyMode(t *testing.T) { + webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + response := `[ + { + "id": "custom", + "title": "Custom", + "count": 10 + }, + { + "id": "custom_logs", + "title": "Custom Logs", + "count": 3, + "parent_id": "custom", + "parent_title": "Custom" + } +]` + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, response) + })) + defer webServer.Close() + + indexerProxy := packages.NewFileSystemIndexer(testLogger, "./testdata/second_package_path") + err := indexerProxy.Init(context.Background()) + require.NoError(t, err) + + proxyMode, err := proxymode.NewProxyMode( + testLogger, + proxymode.ProxyOptions{ + Enabled: true, + ProxyTo: webServer.URL, + }, + ) + require.NoError(t, err) + + categoriesWithProxyHandler := categoriesHandlerWithProxyMode(testLogger, indexerProxy, proxyMode, testCacheTime) + + tests := []struct { + endpoint string + path string + file string + handler func(w http.ResponseWriter, r *http.Request) + }{ + {"/categories", "/categories", "categories-proxy.json", categoriesWithProxyHandler}, + {"/categories?kibana.version=6.5.0", "/categories", "categories-proxy-kibana-filter.json", categoriesWithProxyHandler}, + } + + for _, test := range tests { + t.Run(test.endpoint, func(t *testing.T) { + runEndpoint(t, test.endpoint, test.path, test.file, test.handler) + }) + } +} diff --git a/testdata/generated/categories-proxy-kibana-filter.json b/testdata/generated/categories-proxy-kibana-filter.json new file mode 100644 index 000000000..913fe004b --- /dev/null +++ b/testdata/generated/categories-proxy-kibana-filter.json @@ -0,0 +1,15 @@ +[ + { + "id": "custom", + "title": "Custom", + "count": 10 + }, + { + "id": "custom_logs", + "title": "Custom Logs", + "count": 3, + "parent_id": "custom", + "parent_title": "Custom" + } +] + diff --git a/testdata/generated/categories-proxy.json b/testdata/generated/categories-proxy.json new file mode 100644 index 000000000..a0c51f46f --- /dev/null +++ b/testdata/generated/categories-proxy.json @@ -0,0 +1,22 @@ +[ + { + "id": "custom", + "title": "Custom", + "count": 11 + }, + { + "id": "custom_logs", + "title": "Custom Logs", + "count": 3, + "parent_id": "custom", + "parent_title": "Custom" + }, + { + "id": "web", + "title": "Web Server", + "count": 1, + "parent_id": "observability", + "parent_title": "Observability" + } +] +