Skip to content

Commit b570826

Browse files
author
Steve McDougall
committed
Package ahoy
0 parents  commit b570826

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

.github/workflows/go.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
11+
build:
12+
name: Build
13+
runs-on: ubuntu-latest
14+
steps:
15+
16+
- name: Set up Go 1.x
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: ^1.13
20+
id: go
21+
22+
- name: Check out code into the Go module directory
23+
uses: actions/checkout@v2
24+
25+
- name: Get dependencies
26+
run: |
27+
go get -v -t -d ./...
28+
if [ -f Gopkg.toml ]; then
29+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
30+
dep ensure
31+
fi
32+
33+
- name: Build
34+
run: go build -v .
35+
36+
- name: Test
37+
run: go test -v .

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Go HTTP Response
2+
3+
A simple package to build HTTP Responses in a specific format.

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/JustSteveKing/go-http-response
2+
3+
go 1.13

response.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package response
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
// Response is a generic HTTP Response Struct
9+
type Response struct {
10+
Data string `json:"data"`
11+
}
12+
13+
// Send a HTTP Response
14+
func Send(responseWriter http.ResponseWriter, code int, payload interface{}, contentType string) {
15+
response, _ := json.Marshal(payload)
16+
17+
responseWriter.Header().Set("Content-Type", contentType)
18+
responseWriter.WriteHeader(code)
19+
responseWriter.Write(response)
20+
}

response_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package response
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
func TestResponse(t *testing.T) {
10+
t.Run("Test something", func(t *testing.T) {
11+
handler := func(w http.ResponseWriter, r *http.Request) {
12+
body := &Response{
13+
Data: "foo",
14+
}
15+
Send(w, 200, body, "application/json")
16+
}
17+
18+
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
19+
w := httptest.NewRecorder()
20+
handler(w, req)
21+
})
22+
}

0 commit comments

Comments
 (0)