Skip to content

Commit

Permalink
sourcemap: fix data race in test (#5842)
Browse files Browse the repository at this point in the history
Fix data race in TestFleetFetch: as there are two
servers, one may update hasAuth concurrently with
the test checking the value.

Change the way the auth header is checked to be
similar to how the header would be checked in a
real fleet-server, and respond with 401 if the
auth header is invalid.
  • Loading branch information
axw authored Aug 2, 2021
1 parent 2e1dc7b commit 710d4ed
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions sourcemap/fleet_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"

Expand All @@ -34,8 +33,6 @@ import (

func TestFleetFetch(t *testing.T) {
var (
hasAuth = true
mu sync.Mutex
apikey = "supersecret"
name = "webapp"
version = "1.0.0"
Expand All @@ -46,10 +43,10 @@ func TestFleetFetch(t *testing.T) {

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, sourceMapPath, r.URL.Path)
auth := r.Header.Get("Authorization")
mu.Lock()
hasAuth = hasAuth && (auth == "ApiKey "+apikey)
mu.Unlock()
if auth := r.Header.Get("Authorization"); auth != "ApiKey "+apikey {
w.WriteHeader(http.StatusUnauthorized)
return
}
// zlib compress
wr := zlib.NewWriter(w)
defer wr.Close()
Expand Down Expand Up @@ -84,7 +81,6 @@ func TestFleetFetch(t *testing.T) {
require.NoError(t, err)

assert.Contains(t, gotRes, "webpack:///bundle.js")
assert.True(t, hasAuth)
}

func TestFailedAndSuccessfulFleetHostsFetch(t *testing.T) {
Expand Down

0 comments on commit 710d4ed

Please sign in to comment.