Skip to content

Commit

Permalink
bumped go to 1.19 & added github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvajagtap committed Jul 20, 2023
1 parent 22b2312 commit b3c9a9f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 145 deletions.
70 changes: 0 additions & 70 deletions .circleci/config.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
golangci:
strategy:
matrix:
go: ['1.19','1.20']
os: [ubuntu-latest]
go: ['1.18', '1.19','1.20']
os: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: true
name: verify
runs-on: ${{ matrix.os }}
Expand All @@ -36,4 +36,4 @@ jobs:
args: --timeout=5m

- name: Test
run: go test -race --coverprofile=coverage.coverprofile --covermode=atomic -v ./...
run: make test
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
14 changes: 10 additions & 4 deletions mux_httpserver_test.go
Original file line number Diff line number Diff line change
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

0 comments on commit b3c9a9f

Please sign in to comment.