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.

17 changes: 17 additions & 0 deletions .github/workflows/issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add all the issues created to the project.
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
name: Add all issues to Project

on:
issues:
types:
- opened

jobs:
add-to-project:
runs-on: ubuntu-latest
steps:
- name: Add issue to project
uses: actions/[email protected]
with:
project-url: https://github.com/orgs/gorilla/projects/mux
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
github-token: ${{ secrets.GITHUB_TOKEN }}
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
verify-and-test:
strategy:
matrix:
go: ['1.18', '1.19','1.20']
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
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: Verify
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
args: --timeout=5m

- name: Test
run: make test
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/gorilla/mux

go 1.12
go 1.19
95 changes: 76 additions & 19 deletions middleware_test.go
Original file line number Diff line number Diff line change
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
Loading