Skip to content

Commit

Permalink
Add parent fields for categories returned by the proxy mode (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodm authored May 4, 2023
1 parent fe307b5 commit c54dfbf
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions proxy_mode_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
15 changes: 15 additions & 0 deletions testdata/generated/categories-proxy-kibana-filter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": "custom",
"title": "Custom",
"count": 10
},
{
"id": "custom_logs",
"title": "Custom Logs",
"count": 3,
"parent_id": "custom",
"parent_title": "Custom"
}
]

22 changes: 22 additions & 0 deletions testdata/generated/categories-proxy.json
Original file line number Diff line number Diff line change
@@ -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"
}
]

0 comments on commit c54dfbf

Please sign in to comment.