Skip to content

Commit

Permalink
Package ahoy
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve McDougall committed Jul 12, 2020
0 parents commit b570826
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Go

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: go build -v .

- name: Test
run: go test -v .
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Go HTTP Response

A simple package to build HTTP Responses in a specific format.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/JustSteveKing/go-http-response

go 1.13
20 changes: 20 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package response

import (
"encoding/json"
"net/http"
)

// Response is a generic HTTP Response Struct
type Response struct {
Data string `json:"data"`
}

// Send a HTTP Response
func Send(responseWriter http.ResponseWriter, code int, payload interface{}, contentType string) {
response, _ := json.Marshal(payload)

responseWriter.Header().Set("Content-Type", contentType)
responseWriter.WriteHeader(code)
responseWriter.Write(response)
}
22 changes: 22 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package response

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestResponse(t *testing.T) {
t.Run("Test something", func(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
body := &Response{
Data: "foo",
}
Send(w, 200, body, "application/json")
}

req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
handler(w, req)
})
}

0 comments on commit b570826

Please sign in to comment.