Skip to content

Commit

Permalink
purger: add extra API request tests for extra_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheu committed Apr 8, 2022
1 parent 7eaeee5 commit b7eb3b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/pkg/rpaas/nginx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (m NginxManager) PurgeCache(host, purgePath string, port int32, preservePat
status := false
headers := map[string]string{"Accept-Encoding": encoding}
if len(extraHeaders) > 0 {
for header, _ := range extraHeaders {
for header := range extraHeaders {
headers[header] = extraHeaders.Get(header)
status, err = m.purge(host, purgePath, port, preservePath, headers)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions internal/purge/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -55,6 +56,21 @@ func TestCachePurge(t *testing.T) {
},
},
},
{
name: "success with extra headers",
instance: "sample-rpaasv2",
requestBody: `{"path":"/index.html","preserve_path":false,"extra_headers":{"X-Header":["value"]}}`,
expectedStatus: http.StatusOK,
expectedBody: `{"path":"/index.html","instances_purged":2}`,
cacheManager: fakeCacheManager{
purgeCacheFunc: func(host, path string, port int32, preservePath bool, extraHeaders http.Header) (bool, error) {
if path == "/index.html" && reflect.DeepEqual(extraHeaders["X-Header"], []string{"value"}) {
return true, nil
}
return false, nginxManager.NginxError{Msg: "some nginx error"}
},
},
},
{
name: "no cache key found",
instance: "sample-rpaasv2",
Expand Down Expand Up @@ -154,6 +170,21 @@ func TestCachePurgeBulk(t *testing.T) {
},
},
},
{
name: "success with extra headers",
instance: "sample-rpaasv2",
requestBody: `[{"path":"/index.html","preserve_path":false,"extra_headers":{"X-Header":["value"]}}]`,
expectedStatus: http.StatusOK,
expectedBody: `[{"path":"/index.html","instances_purged":2}]`,
cacheManager: fakeCacheManager{
purgeCacheFunc: func(host, path string, port int32, preservePath bool, extraHeaders http.Header) (bool, error) {
if path == "/index.html" && reflect.DeepEqual(extraHeaders["X-Header"], []string{"value"}) {
return true, nil
}
return false, nginxManager.NginxError{Msg: "some nginx error"}
},
},
},
{
name: "returns bad request if instance is not sent",
instance: "",
Expand Down

0 comments on commit b7eb3b4

Please sign in to comment.