Skip to content

Commit

Permalink
Merge pull request #434 from dunglas/fix/linter
Browse files Browse the repository at this point in the history
ci: fix golanci-lint
  • Loading branch information
dunglas authored Jan 4, 2021
2 parents 6a563f2 + 2c6f701 commit f032121
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 60 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ issues:
- gochecknoglobals
- funlen
- godox
- noctx
68 changes: 34 additions & 34 deletions authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Utb8p35tfj97usdiEB0AN8ray4wZbVWj
`

func TestAuthorizeMultipleAuthorizationHeader(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", validEmptyHeader)
r.Header.Add("Authorization", validEmptyHeader)

Expand All @@ -74,7 +74,7 @@ func TestAuthorizeMultipleAuthorizationHeader(t *testing.T) {
}

func TestAuthorizeMultipleAuthorizationHeaderRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", validEmptyHeaderRsa)
r.Header.Add("Authorization", validEmptyHeaderRsa)

Expand All @@ -84,7 +84,7 @@ func TestAuthorizeMultipleAuthorizationHeaderRsa(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderTooShort(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer x")

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
Expand All @@ -93,7 +93,7 @@ func TestAuthorizeAuthorizationHeaderTooShort(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderNoBearer(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Greater "+validEmptyHeader)

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
Expand All @@ -102,7 +102,7 @@ func TestAuthorizeAuthorizationHeaderNoBearer(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderNoBearerRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Greater "+validEmptyHeaderRsa)

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
Expand All @@ -111,7 +111,7 @@ func TestAuthorizeAuthorizationHeaderNoBearerRsa(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderInvalidAlg(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+createDummyNoneSignedJWT())

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
Expand All @@ -120,7 +120,7 @@ func TestAuthorizeAuthorizationHeaderInvalidAlg(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderInvalidKey(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validEmptyHeader)

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
Expand All @@ -129,7 +129,7 @@ func TestAuthorizeAuthorizationHeaderInvalidKey(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderInvalidKeyRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validEmptyHeaderRsa)

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
Expand All @@ -138,7 +138,7 @@ func TestAuthorizeAuthorizationHeaderInvalidKeyRsa(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderNoContent(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validEmptyHeader)

claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
Expand All @@ -148,7 +148,7 @@ func TestAuthorizeAuthorizationHeaderNoContent(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderNoContentRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validEmptyHeaderRsa)

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -158,7 +158,7 @@ func TestAuthorizeAuthorizationHeaderNoContentRsa(t *testing.T) {
}

func TestAuthorizeAuthorizationHeader(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validFullHeader)

claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
Expand All @@ -168,7 +168,7 @@ func TestAuthorizeAuthorizationHeader(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validFullHeaderRsa)

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -178,7 +178,7 @@ func TestAuthorizeAuthorizationHeaderRsa(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderNamespacedRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validFullHeaderNamespacedRsa)

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -188,7 +188,7 @@ func TestAuthorizeAuthorizationHeaderNamespacedRsa(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderRsaWithCert(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validFullHeaderRsaForCert)

claims, err := authorize(r, &jwtConfig{[]byte(certificateRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -198,7 +198,7 @@ func TestAuthorizeAuthorizationHeaderRsaWithCert(t *testing.T) {
}

func TestAuthorizeAuthorizationHeaderWrongAlgorithm(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.Header.Add("Authorization", "Bearer "+validFullHeaderRsa)

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), nil}, []string{})
Expand All @@ -207,7 +207,7 @@ func TestAuthorizeAuthorizationHeaderWrongAlgorithm(t *testing.T) {
}

func TestAuthorizeCookieInvalidAlg(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: createDummyNoneSignedJWT()})

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
Expand All @@ -216,7 +216,7 @@ func TestAuthorizeCookieInvalidAlg(t *testing.T) {
}

func TestAuthorizeCookieInvalidKey(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeader})

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodHS256}, []string{})
Expand All @@ -225,7 +225,7 @@ func TestAuthorizeCookieInvalidKey(t *testing.T) {
}

func TestAuthorizeCookieEmptyKeyRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeaderRsa})

claims, err := authorize(r, &jwtConfig{[]byte{}, jwt.SigningMethodRS256}, []string{})
Expand All @@ -234,7 +234,7 @@ func TestAuthorizeCookieEmptyKeyRsa(t *testing.T) {
}

func TestAuthorizeCookieInvalidKeyRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeaderRsa})

claims, err := authorize(r, &jwtConfig{[]byte(privateKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -243,7 +243,7 @@ func TestAuthorizeCookieInvalidKeyRsa(t *testing.T) {
}

func TestAuthorizeCookieNoContent(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeader})

claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
Expand All @@ -253,7 +253,7 @@ func TestAuthorizeCookieNoContent(t *testing.T) {
}

func TestAuthorizeCookieNoContentRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validEmptyHeaderRsa})

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -263,7 +263,7 @@ func TestAuthorizeCookieNoContentRsa(t *testing.T) {
}

func TestAuthorizeCookie(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})

claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
Expand All @@ -273,7 +273,7 @@ func TestAuthorizeCookie(t *testing.T) {
}

func TestAuthorizeCookieRsa(t *testing.T) {
r, _ := http.NewRequest("GET", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("GET", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -283,7 +283,7 @@ func TestAuthorizeCookieRsa(t *testing.T) {
}

func TestAuthorizeCookieNoOriginNoReferer(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})

claims, err := authorize(r, &jwtConfig{[]byte("!ChangeMe!"), jwt.SigningMethodHS256}, []string{})
Expand All @@ -292,7 +292,7 @@ func TestAuthorizeCookieNoOriginNoReferer(t *testing.T) {
}

func TestAuthorizeCookieNoOriginNoRefererRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})

claims, err := authorize(r, &jwtConfig{[]byte(publicKeyRsa), jwt.SigningMethodRS256}, []string{})
Expand All @@ -301,7 +301,7 @@ func TestAuthorizeCookieNoOriginNoRefererRsa(t *testing.T) {
}

func TestAuthorizeCookieOriginNotAllowed(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})

Expand All @@ -311,7 +311,7 @@ func TestAuthorizeCookieOriginNotAllowed(t *testing.T) {
}

func TestAuthorizeCookieOriginNotAllowedRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})

Expand All @@ -321,7 +321,7 @@ func TestAuthorizeCookieOriginNotAllowedRsa(t *testing.T) {
}

func TestAuthorizeCookieRefererNotAllowed(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Referer", "http://example.com/foo/bar")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})

Expand All @@ -331,7 +331,7 @@ func TestAuthorizeCookieRefererNotAllowed(t *testing.T) {
}

func TestAuthorizeCookieRefererNotAllowedRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Referer", "http://example.com/foo/bar")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})

Expand All @@ -341,7 +341,7 @@ func TestAuthorizeCookieRefererNotAllowedRsa(t *testing.T) {
}

func TestAuthorizeCookieInvalidReferer(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Referer", "http://192.168.0.%31/")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})

Expand All @@ -351,7 +351,7 @@ func TestAuthorizeCookieInvalidReferer(t *testing.T) {
}

func TestAuthorizeCookieInvalidRefererRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Referer", "http://192.168.0.%31/")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})

Expand All @@ -361,7 +361,7 @@ func TestAuthorizeCookieInvalidRefererRsa(t *testing.T) {
}

func TestAuthorizeCookieOriginHasPriority(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Origin", "http://example.net")
r.Header.Add("Referer", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})
Expand All @@ -373,7 +373,7 @@ func TestAuthorizeCookieOriginHasPriority(t *testing.T) {
}

func TestAuthorizeCookieOriginHasPriorityRsa(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Origin", "http://example.net")
r.Header.Add("Referer", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeaderRsa})
Expand All @@ -385,7 +385,7 @@ func TestAuthorizeCookieOriginHasPriorityRsa(t *testing.T) {
}

func TestAuthorizeAllOriginsAllowed(t *testing.T) {
r, _ := http.NewRequest("POST", defaultHubURL, nil) //nolint:noctx
r, _ := http.NewRequest("POST", defaultHubURL, nil)
r.Header.Add("Origin", "http://example.com")
r.AddCookie(&http.Cookie{Name: "mercureAuthorization", Value: validFullHeader})

Expand Down
4 changes: 2 additions & 2 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestMercure(t *testing.T) {
connected.Wait()

body := url.Values{"topic": {"http://example.com/foo/1"}, "data": {"bar"}, "id": {"bar"}}
req, err := http.NewRequest("POST", "https://localhost:9080/.well-known/mercure", strings.NewReader(body.Encode())) //nolint:noctx
req, err := http.NewRequest("POST", "https://localhost:9080/.well-known/mercure", strings.NewReader(body.Encode()))
require.Nil(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "Bearer "+publisherJWT)
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestJWTPlaceholders(t *testing.T) {
connected.Wait()

body := url.Values{"topic": {"http://example.com/foo/1"}, "data": {"bar"}, "id": {"bar"}}
req, err := http.NewRequest("POST", "https://localhost:9080/.well-known/mercure", strings.NewReader(body.Encode())) //nolint:noctx
req, err := http.NewRequest("POST", "https://localhost:9080/.well-known/mercure", strings.NewReader(body.Encode()))
require.Nil(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "Bearer "+publisherJWTRSA)
Expand Down
2 changes: 2 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func (s *MemorySink) Sync() error { return nil }
var sink *MemorySink

func newTestLogger(t *testing.T) (*MemorySink, Logger) {
t.Helper()

if sink == nil {
sink = &MemorySink{new(bytes.Buffer)}
if err := zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) {
Expand Down
4 changes: 4 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func TestTotalOfHandledUpdates(t *testing.T) {
}

func assertGaugeLabelValue(t *testing.T, v float64, g *prometheus.GaugeVec, l string) {
t.Helper()

var metricOut dto.Metric

m, err := g.GetMetricWithLabelValues(l)
Expand All @@ -97,6 +99,8 @@ func assertGaugeLabelValue(t *testing.T, v float64, g *prometheus.GaugeVec, l st
}

func assertCounterValue(t *testing.T, v float64, c *prometheus.CounterVec, l string) {
t.Helper()

var metricOut dto.Metric

m, err := c.GetMetricWithLabelValues(l)
Expand Down
Loading

0 comments on commit f032121

Please sign in to comment.