Skip to content

Commit a81ab1c

Browse files
author
Dean Karn
authored
Additions (#6)
1 parent 7431e1b commit a81ab1c

File tree

6 files changed

+39
-27
lines changed

6 files changed

+39
-27
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.14.x,1.13.x]
7+
go-version: [1.15.x,1.14.x,1.13.x]
88
platform: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/checkout@v2
2626

2727
- name: Lint
28-
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.14.x'
28+
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.15.x'
2929
run: |
3030
export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
3131
go get -u golang.org/x/lint/golint

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pkg
2-
![Project status](https://img.shields.io/badge/version-5.1.0-green.svg)
2+
![Project status](https://img.shields.io/badge/version-5.2.0-green.svg)
33
[![Build Status](https://travis-ci.org/go-playground/pkg.svg?branch=master)](https://travis-ci.org/go-playground/pkg)
44
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
55
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ require (
66
github.com/go-playground/form/v4 v4.1.1
77
)
88

9-
go 1.14
9+
go 1.15

net/http/helpers.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func detectContentType(filename string) string {
3636
case ".md":
3737
return TextMarkdown
3838
default:
39-
return OctetStream
39+
return ApplicationOctetStream
4040
}
4141
}
4242

@@ -102,6 +102,17 @@ func ClientIP(r *http.Request) (clientIP string) {
102102
return
103103
}
104104

105+
//
106+
// JSONStream uses json.Encoder to stream the JSON reponse body.
107+
//
108+
// This differs from the JSON helper which unmarshalls into memory first allowing the capture of JSON encoding errors.
109+
//
110+
func JSONStream(w http.ResponseWriter, status int, i interface{}) error {
111+
w.Header().Set(ContentType, ApplicationJSON)
112+
w.WriteHeader(status)
113+
return json.NewEncoder(w).Encode(i)
114+
}
115+
105116
// JSON marshals provided interface + returns JSON + status code
106117
func JSON(w http.ResponseWriter, status int, i interface{}) error {
107118
b, err := json.Marshal(i)

net/http/helpers_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestAttachment(t *testing.T) {
7373
{
7474
code: http.StatusOK,
7575
disposition: "attachment;filename=readme",
76-
typ: OctetStream,
76+
typ: ApplicationOctetStream,
7777
url: "/dl-unknown-type",
7878
},
7979
{
@@ -136,7 +136,7 @@ func TestInline(t *testing.T) {
136136
{
137137
code: http.StatusOK,
138138
disposition: "inline;filename=readme",
139-
typ: OctetStream,
139+
typ: ApplicationOctetStream,
140140
url: "/dl-unknown-type-inline",
141141
},
142142
}
@@ -192,7 +192,7 @@ func TestJSON(t *testing.T) {
192192
err = JSON(w, http.StatusOK, tst)
193193
Equal(t, err, nil)
194194
Equal(t, w.Header().Get(ContentType), ApplicationJSON)
195-
Equal(t, w.Body.Bytes(), append([]byte(b)))
195+
Equal(t, w.Body.Bytes(), b)
196196

197197
err = JSON(w, http.StatusOK, func() {})
198198
NotEqual(t, err, nil)
@@ -210,7 +210,7 @@ func TestJSONBytes(t *testing.T) {
210210
err = JSONBytes(w, http.StatusOK, b)
211211
Equal(t, err, nil)
212212
Equal(t, w.Header().Get(ContentType), ApplicationJSON)
213-
Equal(t, w.Body.Bytes(), []byte(b))
213+
Equal(t, w.Body.Bytes(), b)
214214
}
215215

216216
func TestJSONP(t *testing.T) {

net/http/mime_types.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ const (
66

77
// Mime Type values for the Content-Type HTTP header
88
const (
9-
ApplicationJSON string = "application/json" + charsetUTF8
10-
ApplicationJavaScript string = "application/javascript"
11-
ApplicationXML string = "application/xml" + charsetUTF8
12-
ApplicationForm string = "application/x-www-form-urlencoded"
13-
ApplicationProtobuf string = "application/protobuf"
14-
ApplicationMsgpack string = "application/msgpack"
15-
ApplicationWasm string = "application/wasm"
16-
ApplicationPDF string = "application/pdf"
17-
TextHTML string = "text/html" + charsetUTF8
18-
TextPlain string = "text/plain" + charsetUTF8
19-
TextMarkdown string = "text/markdown" + charsetUTF8
20-
TextCSS string = "text/css" + charsetUTF8
21-
ImagePNG string = "image/png"
22-
ImageGIF string = "image/gif"
23-
ImageSVG string = "image/svg+xml"
24-
ImageJPEG string = "image/jpeg"
25-
MultipartForm string = "multipart/form-data"
26-
OctetStream string = "application/octet-stream"
9+
ApplicationJSON string = "application/json" + charsetUTF8
10+
ApplicationJavaScript string = "application/javascript"
11+
ApplicationXML string = "application/xml" + charsetUTF8
12+
ApplicationForm string = "application/x-www-form-urlencoded"
13+
ApplicationProtobuf string = "application/protobuf"
14+
ApplicationMsgpack string = "application/msgpack"
15+
ApplicationWasm string = "application/wasm"
16+
ApplicationPDF string = "application/pdf"
17+
ApplicationOctetStream string = "application/octet-stream"
18+
TextHTML string = "text/html" + charsetUTF8
19+
TextPlain string = "text/plain" + charsetUTF8
20+
TextMarkdown string = "text/markdown" + charsetUTF8
21+
TextCSS string = "text/css" + charsetUTF8
22+
TextCSV string = "text/csv"
23+
ImagePNG string = "image/png"
24+
ImageGIF string = "image/gif"
25+
ImageSVG string = "image/svg+xml"
26+
ImageJPEG string = "image/jpeg"
27+
MultipartForm string = "multipart/form-data"
2728
)

0 commit comments

Comments
 (0)