Skip to content

Commit

Permalink
backfill cache control header tests for ui assets
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Windsor <[email protected]>
  • Loading branch information
sentientmonkey committed Sep 2, 2023
1 parent ef7f32e commit 01e0727
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
8 changes: 4 additions & 4 deletions server/server_norace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestUserAgent(t *testing.T) {
// the data race, it APPEARS to be intentional, but in any case it's nothing we are doing in Argo CD
// that is causing this issue.

s, closer := fakeServer()
s, closer := fakeServer(t)
defer closer()
lns, err := s.Listen()
assert.NoError(t, err)
Expand Down Expand Up @@ -94,7 +94,7 @@ func Test_StaticHeaders(t *testing.T) {

// Test default policy "sameorigin" and "frame-ancestors 'self';"
{
s, closer := fakeServer()
s, closer := fakeServer(t)
defer closer()
lns, err := s.Listen()
assert.NoError(t, err)
Expand All @@ -121,7 +121,7 @@ func Test_StaticHeaders(t *testing.T) {

// Test custom policy for X-Frame-Options and Content-Security-Policy
{
s, closer := fakeServer()
s, closer := fakeServer(t)
defer closer()
s.XFrameOptions = "deny"
s.ContentSecurityPolicy = "frame-ancestors 'none';"
Expand Down Expand Up @@ -150,7 +150,7 @@ func Test_StaticHeaders(t *testing.T) {

// Test disabled X-Frame-Options and Content-Security-Policy
{
s, closer := fakeServer()
s, closer := fakeServer(t)
defer closer()
s.XFrameOptions = ""
s.ContentSecurityPolicy = ""
Expand Down
86 changes: 81 additions & 5 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -37,14 +39,20 @@ import (
testutil "github.com/argoproj/argo-cd/v2/util/test"
)

func fakeServer() (*ArgoCDServer, func()) {
type FakeArgoCDServer struct {
*ArgoCDServer
TmpAssetsDir string
}

func fakeServer(t *testing.T) (*FakeArgoCDServer, func()) {
cm := test.NewFakeConfigMap()
secret := test.NewFakeSecret()
kubeclientset := fake.NewSimpleClientset(cm, secret)
appClientSet := apps.NewSimpleClientset()
redis, closer := test.NewInMemoryRedis()
port, err := test.GetFreePort()
mockRepoClient := &mocks.Clientset{RepoServerServiceClient: &mocks.RepoServerServiceClient{}}
tmpAssetsDir := t.TempDir()

if err != nil {
panic(err)
Expand All @@ -68,11 +76,13 @@ func fakeServer() (*ArgoCDServer, func()) {
1*time.Minute,
1*time.Minute,
),
RedisClient: redis,
RepoClientset: mockRepoClient,
RedisClient: redis,
RepoClientset: mockRepoClient,
StaticAssetsDir: tmpAssetsDir,
}
srv := NewServer(context.Background(), argoCDOpts)
return srv, closer
fakeSrv := &FakeArgoCDServer{srv, tmpAssetsDir}
return fakeSrv, closer
}

func TestEnforceProjectToken(t *testing.T) {
Expand Down Expand Up @@ -393,7 +403,7 @@ func TestRevokedToken(t *testing.T) {
}

func TestCertsAreNotGeneratedInInsecureMode(t *testing.T) {
s, closer := fakeServer()
s, closer := fakeServer(t)
defer closer()
assert.True(t, s.Insecure)
assert.Nil(t, s.settings.Certificate)
Expand Down Expand Up @@ -1230,6 +1240,72 @@ func TestIsMainJsBundle(t *testing.T) {
}
}

func TestCacheControlHeaders(t *testing.T) {
testCases := []struct {
name string
filename string
createFile bool
expectedStatus int
expectedCacheControlHeaders []string
}{
{
name: "file exists",
filename: "exists.html",
createFile: true,
expectedStatus: 200,
expectedCacheControlHeaders: nil,
},
{
name: "file does not exist",
filename: "missing.html",
createFile: false,
expectedStatus: 404,
expectedCacheControlHeaders: nil,
},
{
name: "main js bundle exists",
filename: "main.e4188e5adc97bbfc00c3.js",
createFile: true,
expectedStatus: 200,
expectedCacheControlHeaders: []string{"public, max-age=31536000, immutable"},
},
{
name: "main js bundle does not exists",
filename: "main.e4188e5adc97bbfc00c0.js",
createFile: false,
expectedStatus: 404,
expectedCacheControlHeaders: []string{"public, max-age=31536000, immutable"},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
argocd, closer := fakeServer(t)
defer closer()

handler := argocd.newStaticAssetsHandler()

rr := httptest.NewRecorder()
req := httptest.NewRequest("", fmt.Sprintf("/%s", testCase.filename), nil)

fp := filepath.Join(argocd.TmpAssetsDir, testCase.filename)

if testCase.createFile {
tmpFile, err := os.Create(fp)
assert.NoError(t, err)
err = tmpFile.Close()
assert.NoError(t, err)
}

handler(rr, req)

assert.Equal(t, testCase.expectedStatus, rr.Code)

cacheControl := rr.HeaderMap["Cache-Control"]
assert.Equal(t, testCase.expectedCacheControlHeaders, cacheControl)
})
}
}
func TestReplaceBaseHRef(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit 01e0727

Please sign in to comment.