Skip to content

Commit

Permalink
update go version & add verification/testing tools (#34)
Browse files Browse the repository at this point in the history
<!--
For Work In Progress Pull Requests, please use the Draft PR feature,
see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for
further details.

     For a timely review/response, please avoid force-pushing additional
     commits if your PR already received reviews or comments.

     Before submitting a Pull Request, please ensure that you have:
- 📖 Read the Contributing guide:
https://github.com/gorilla/.github/blob/main/CONTRIBUTING.md
- 📖 Read the Code of Conduct:
https://github.com/gorilla/.github/blob/main/CODE_OF_CONDUCT.md

     - Provide tests for your changes.
     - Use descriptive commit messages.
	 - Comment your code where appropriate.
	 - Squash your commits
     - Update any related documentation.

     - Add gorilla/pull-request-reviewers as a Reviewer
-->

## What type of PR is this? (check all applicable)

- [ ] Refactor
- [ ] Feature
- [ ] Bug Fix
- [x] Optimization
- [x] Documentation Update

## Description

## Related Tickets & Documents

<!--
For pull requests that relate or close an issue, please include them
below. We like to follow [Github's guidance on linking issues to pull
requests](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue).

For example having the text: "closes #1234" would connect the current
pull
request to issue 1234.  And when we merge the pull request, Github will
automatically close the issue.
-->

- Related Issue #
- Closes #

## Added/updated tests?

- [x] Yes

## Run verifications and test

- [x] `make verify` is passing
- [] `make test` is passing
  • Loading branch information
apoorvajagtap authored Aug 25, 2023
1 parent a3390cc commit d8f0368
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 137 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; https://editorconfig.org/

root = true

[*]
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
indent_style = tab
indent_size = 4

[*.md]
indent_size = 4
trim_trailing_whitespace = false

eclint_indent_style = unset
21 changes: 21 additions & 0 deletions .github/workflows/issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add issues or pull-requests created to the project.
name: Add issue or pull request to Project

on:
issues:
types:
- opened
pull_request_target:
types:
- opened
- reopened

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/4
github-token: ${{ secrets.ADD_TO_PROJECT_TOKEN }}
55 changes: 55 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
verify-and-test:
strategy:
matrix:
go: ['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: Run GolangCI-Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
args: --timeout=5m

- name: Run GoSec
if: matrix.os == 'ubuntu-latest'
uses: securego/gosec@master
with:
args: ./...

- name: Run GoVulnCheck
uses: golang/govulncheck-action@v1
with:
go-version-input: ${{ matrix.go }}
go-package: ./...

- name: Run Tests
run: go test -race -cover -coverprofile=coverage -covermode=atomic -v ./...

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./coverage
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage.coverprofile
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

3 changes: 0 additions & 3 deletions AUTHORS

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013 Dave Cheney. All rights reserved.
Copyright (c) 2023 The Gorilla Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Expand Down
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
GO_LINT=$(shell which golangci-lint 2> /dev/null || echo '')
GO_LINT_URI=github.com/golangci/golangci-lint/cmd/golangci-lint@latest

GO_SEC=$(shell which gosec 2> /dev/null || echo '')
GO_SEC_URI=github.com/securego/gosec/v2/cmd/gosec@latest

GO_VULNCHECK=$(shell which govulncheck 2> /dev/null || echo '')
GO_VULNCHECK_URI=golang.org/x/vuln/cmd/govulncheck@latest

.PHONY: golangci-lint
golangci-lint:
$(if $(GO_LINT), ,go install $(GO_LINT_URI))
@echo "##### Running golangci-lint"
golangci-lint run -v

.PHONY: gosec
gosec:
$(if $(GO_SEC), ,go install $(GO_SEC_URI))
@echo "##### Running gosec"
gosec ./...

.PHONY: govulncheck
govulncheck:
$(if $(GO_VULNCHECK), ,go install $(GO_VULNCHECK_URI))
@echo "##### Running govulncheck"
govulncheck ./...

.PHONY: verify
verify: golangci-lint gosec govulncheck

.PHONY: test
test:
@echo "##### Running tests"
go test -race -cover -coverprofile=coverage.coverprofile -covermode=atomic -v ./...
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## gorilla/http
# gorilla/http

![testing](https://github.com/gorilla/http/actions/workflows/test.yml/badge.svg)
[![codecov](https://codecov.io/github/gorilla/http/branch/main/graph/badge.svg)](https://codecov.io/github/gorilla/http)
[![godoc](https://godoc.org/github.com/gorilla/http?status.svg)](https://godoc.org/github.com/gorilla/http)
[![sourcegraph](https://sourcegraph.com/github.com/gorilla/http/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/http?badge)

A simple, safe and powerful HTTP client for the Go language.

Build Status: [![Build Status](https://travis-ci.org/gorilla/http.png?branch=master)](https://travis-ci.org/gorilla/http)
![Gorilla Logo](https://github.com/gorilla/.github/assets/53367916/d92caabf-98e0-473e-bfbf-ab554ba435e5)

A simple, safe and powerful HTTP client for the Go language.

# Image of an under construction GIF

Expand Down
5 changes: 2 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
stdurl "net/url"
"strings"

Expand Down Expand Up @@ -61,7 +60,7 @@ func (c *Client) Do(method, url string, headers map[string][]string, body io.Rea
rc := &readCloser{rbody, conn}
if rstatus.IsRedirect() && c.FollowRedirects {
// consume the response body
_, err := io.Copy(ioutil.Discard, rc)
_, err := io.Copy(io.Discard, rc)
if err := firstErr(err, rc.Close()); err != nil {
return client.Status{}, nil, nil, err // TODO
}
Expand Down Expand Up @@ -134,7 +133,7 @@ func toHeaders(h map[string][]string) []client.Header {
var r []client.Header
for k, v := range h {
for _, v := range v {
r = append(r, client.Header{k, v})
r = append(r, client.Header{Key: k, Value: v})
}
}
return r
Expand Down
43 changes: 25 additions & 18 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,23 @@ var readResponseTests = []struct {
Status: Status{200, "OK"},
Headers: []Header{{"Host", "localhost"}, {"Connection", "close"}},
}, io.EOF},
// excessively long chunk.
{"HTTP/1.1 200 Ok\r\nTransfer-encoding: chunked\r\n\r\n" +
"004\r\n1234\r\n" +
"1000000000000000000001\r\n@\r\n" +
"00000000\r\n" +
"\r\n",
&Response{
Version: HTTP_1_1,
Status: Status{200, "Ok"},
Headers: []Header{{"Transfer-encoding", "chunked"}},
Body: b("1234@"),
},
nil,
},
// The following test case no longer meets the expectations as per
// the conditions introduced with go version >=1.6.
// (ref: https://github.com/golang/go/blob/release-branch.go1.6/src/net/http/internal/chunked.go#L234)
//
// {"HTTP/1.1 200 Ok\r\nTransfer-encoding: chunked\r\n\r\n" +
// "004\r\n1234\r\n" +
// "1000000000000000000001\r\n@\r\n" +
// "00000000\r\n" +
// "\r\n",
// &Response{
// Version: HTTP_1_1,
// Status: Status{200, "Ok"},
// Headers: []Header{{"Transfer-encoding", "chunked"}},
// Body: b("1234@"),
// },
// nil,
// },
// silly redirect header
{
"HTTP/1.1 302 WebCenter Redirect\r\n" +
Expand Down Expand Up @@ -281,12 +284,16 @@ func TestClientReadResponse(t *testing.T) {
var buf bytes.Buffer
var expected, actual string
if tt.Response.Body != nil {
_, err = io.Copy(&buf, tt.Response.Body)
if _, err := io.Copy(&buf, tt.Response.Body); err != nil {
t.Fatal(err)
}
expected = buf.String()
}
buf.Reset()
if resp.Body != nil {
_, err = io.Copy(&buf, resp.Body)
if _, err = io.Copy(&buf, resp.Body); err != nil {
t.Fatal(err)
}
actual = buf.String()
}
if actual != expected {
Expand Down Expand Up @@ -338,7 +345,7 @@ func TestRequestCloseRequested(t *testing.T) {
t.Fatal(err)
}
if actual := resp.CloseRequested(); actual != tt.expected {
t.Errorf("ReadResponse(%q): CloseRequested: expected %d got %d", tt.data, tt.expected, actual)
t.Errorf("ReadResponse(%q): CloseRequested: expected %v got %v", tt.data, tt.expected, actual)
}
}
}
Expand All @@ -363,7 +370,7 @@ func TestTransferEncoding(t *testing.T) {
t.Fatal(err)
}
if actual := resp.TransferEncoding(); actual != tt.expected {
t.Errorf("ReadResponse(%q): TransferEncoding: expected %d got %d", tt.data, tt.expected, actual)
t.Errorf("ReadResponse(%q): TransferEncoding: expected %v got %v", tt.data, tt.expected, actual)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions client/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ NEXT:
}
}

var readBodyTests = []struct {
var readBodyTests = []struct { // nolint:unused
body string
length int
expected string
Expand All @@ -170,7 +170,7 @@ var readBodyTests = []struct {
}

// disabled til I know what ReadBody should look like
func testReadBody(t *testing.T) {
func testReadBody(t *testing.T) { // nolint:unused
for _, tt := range readBodyTests {
c := &reader{b(tt.body)}
r := c.ReadBody()
Expand Down
6 changes: 3 additions & 3 deletions client/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type Status struct {
Reason string
}

var invalidStatus Status

func (s Status) String() string { return fmt.Sprintf("%d %s", s.Code, s.Reason) }

func (s Status) IsInformational() bool { return s.Code >= INFO_CONTINUE && s.Code < SUCCESS_OK }
func (s Status) IsSuccess() bool { return s.Code >= SUCCESS_OK && s.Code < REDIRECTION_MULTIPLE_CHOICES }
func (s Status) IsSuccess() bool {
return s.Code >= SUCCESS_OK && s.Code < REDIRECTION_MULTIPLE_CHOICES
}
func (s Status) IsRedirect() bool {
return s.Code >= REDIRECTION_MULTIPLE_CHOICES && s.Code < CLIENT_ERROR_BAD_REQUEST
}
Expand Down
19 changes: 13 additions & 6 deletions client/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"strings"
"testing"
)
Expand Down Expand Up @@ -106,7 +105,9 @@ func TestStartBody(t *testing.T) {
if err := c.WriteHeader("Host", "localhost"); err != nil {
t.Fatal(err)
}
c.StartBody()
if err := c.StartBody(); err != nil {
t.Fatal(err)
}
err := c.WriteHeader("Connection", "close")
if _, ok := err.(*phaseError); !ok {
t.Fatalf("expected %T, got %v", new(phaseError), err)
Expand All @@ -124,7 +125,9 @@ func TestStartBody(t *testing.T) {
func TestDoubleWriteBody(t *testing.T) {
var b bytes.Buffer
c := &writer{Writer: bufio.NewWriter(&b), tmp: &b}
c.StartBody()
if err := c.StartBody(); err != nil {
t.Fatal(err)
}
if err := c.WriteBody(strings.NewReader("")); err != nil {
t.Fatal(err)
}
Expand All @@ -138,7 +141,9 @@ func TestDoubleWriteBody(t *testing.T) {
func TestDoubleWriteChunked(t *testing.T) {
var b bytes.Buffer
c := &writer{Writer: bufio.NewWriter(&b), tmp: &b}
c.StartBody()
if err := c.StartBody(); err != nil {
t.Fatal(err)
}
if err := c.WriteChunked(strings.NewReader("")); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -170,7 +175,9 @@ func (c *writer) write(t *testing.T, w writeTest) {
t.Fatal(err)
}
}
c.StartBody()
if err := c.StartBody(); err != nil {
t.Fatal(err)
}
if err := c.WriteBody(b(w.body)); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -339,7 +346,7 @@ func (w *countingWriter) Write(buf []byte) (int, error) {
// verify that header buffering works
func TestHeaderBuffering(t *testing.T) {
for _, tt := range headerBufferingTests {
cw := countingWriter{Writer: ioutil.Discard}
cw := countingWriter{Writer: io.Discard}
w := &writer{Writer: &cw}
if err := tt.f(w); err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit d8f0368

Please sign in to comment.