Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GPT-95] Update go version, add tools for verification and testing #718

Merged
merged 10 commits into from Jul 23, 2023
70 changes: 0 additions & 70 deletions .circleci/config.yml

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,39 @@
name: Verify and Test
apoorvajagtap marked this conversation as resolved.
Show resolved Hide resolved
on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
golangci:
apoorvajagtap marked this conversation as resolved.
Show resolved Hide resolved
strategy:
matrix:
go: ['1.18', '1.19','1.20']
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
name: verify
apoorvajagtap marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup Go ${{ matrix.go }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
cache: false

- name: Golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
args: --timeout=5m
apoorvajagtap marked this conversation as resolved.
Show resolved Hide resolved

- name: Test
run: make test
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions Makefile
@@ -0,0 +1,33 @@
SHELL := /bin/bash

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# LINT is the path to the golangci-lint binary
LINT = $(shell which golangci-lint)

.PHONY: golangci-lint
golangci-lint:
ifeq (, $(LINT))
ifeq (, $(shell which golangci-lint))
@{ \
set -e ;\
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest ;\
}
override LINT=$(GOBIN)/golangci-lint
else
override LINT=$(shell which golangci-lint)
endif
endif

.PHONY: verify
verify: golangci-lint
$(LINT) run

.PHONY: test
test:
go test -race --coverprofile=coverage.coverprofile --covermode=atomic -v ./...
2 changes: 1 addition & 1 deletion go.mod
@@ -1,3 +1,3 @@
module github.com/gorilla/mux

go 1.12
go 1.19
95 changes: 76 additions & 19 deletions middleware_test.go
Expand Up @@ -158,7 +158,10 @@ func TestMiddlewareExecution(t *testing.T) {

router := NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})

t.Run("responds normally without middleware", func(t *testing.T) {
Expand All @@ -178,7 +181,10 @@ func TestMiddlewareExecution(t *testing.T) {

router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(mwStr)
_, err := w.Write(mwStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})
Expand All @@ -196,11 +202,17 @@ func TestMiddlewareNotFound(t *testing.T) {

router := NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})
router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(mwStr)
_, err := w.Write(mwStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})
Expand All @@ -221,7 +233,10 @@ func TestMiddlewareNotFound(t *testing.T) {
req := newRequest("GET", "/notfound")

router.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Custom 404 handler"))
_, err := rw.Write([]byte("Custom 404 handler"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})
router.ServeHTTP(rw, req)

Expand All @@ -237,12 +252,18 @@ func TestMiddlewareMethodMismatch(t *testing.T) {

router := NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
}).Methods("GET")

router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(mwStr)
_, err := w.Write(mwStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})
Expand All @@ -262,7 +283,10 @@ func TestMiddlewareMethodMismatch(t *testing.T) {
req := newRequest("POST", "/")

router.MethodNotAllowedHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Method not allowed"))
_, err := rw.Write([]byte("Method not allowed"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})
router.ServeHTTP(rw, req)

Expand All @@ -278,17 +302,26 @@ func TestMiddlewareNotFoundSubrouter(t *testing.T) {

router := NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})

subrouter := router.PathPrefix("/sub/").Subrouter()
subrouter.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})

router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(mwStr)
_, err := w.Write(mwStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})
Expand All @@ -308,7 +341,10 @@ func TestMiddlewareNotFoundSubrouter(t *testing.T) {
req := newRequest("GET", "/sub/notfound")

subrouter.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Custom 404 handler"))
_, err := rw.Write([]byte("Custom 404 handler"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})
router.ServeHTTP(rw, req)

Expand All @@ -324,17 +360,26 @@ func TestMiddlewareMethodMismatchSubrouter(t *testing.T) {

router := NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})

subrouter := router.PathPrefix("/sub/").Subrouter()
subrouter.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
w.Write(handlerStr)
_, err := w.Write(handlerStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
}).Methods("GET")

router.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(mwStr)
_, err := w.Write(mwStr)
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})
Expand All @@ -354,7 +399,10 @@ func TestMiddlewareMethodMismatchSubrouter(t *testing.T) {
req := newRequest("POST", "/sub/")

router.MethodNotAllowedHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("Method not allowed"))
_, err := rw.Write([]byte("Method not allowed"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})
router.ServeHTTP(rw, req)

Expand Down Expand Up @@ -508,7 +556,10 @@ func TestMiddlewareOnMultiSubrouter(t *testing.T) {
secondSubRouter := router.PathPrefix("/").Subrouter()

router.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(notFound))
_, err := rw.Write([]byte(notFound))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})

firstSubRouter.HandleFunc("/first", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -521,14 +572,20 @@ func TestMiddlewareOnMultiSubrouter(t *testing.T) {

firstSubRouter.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(first))
_, err := w.Write([]byte(first))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})

secondSubRouter.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(second))
_, err := w.Write([]byte(second))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
h.ServeHTTP(w, r)
})
})
Expand Down
14 changes: 10 additions & 4 deletions mux_httpserver_test.go
Expand Up @@ -5,7 +5,7 @@ package mux

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -14,10 +14,16 @@ import (
func TestSchemeMatchers(t *testing.T) {
router := NewRouter()
router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("hello http world"))
_, err := rw.Write([]byte("hello http world"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
}).Schemes("http")
router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("hello https world"))
_, err := rw.Write([]byte("hello https world"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
}).Schemes("https")

assertResponseBody := func(t *testing.T, s *httptest.Server, expectedBody string) {
Expand All @@ -28,7 +34,7 @@ func TestSchemeMatchers(t *testing.T) {
if resp.StatusCode != 200 {
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected error reading body: %v", err)
}
Expand Down