Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit 8ea0785

Browse files
committed
github.com/qiniu/x v8.0.1; github.com/qiniu/dyn v2.0.1
1 parent d1b7c04 commit 8ea0785

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

context_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package httptest
22

33
import (
4+
"encoding/json"
45
"io"
56
"log"
67
"net/http"
78
"reflect"
89
"strconv"
910
"testing"
1011

11-
"github.com/qiniu/http/httputil"
1212
"github.com/qiniu/x/mockhttp"
1313
)
1414

@@ -33,15 +33,43 @@ func (p *mockTestingT) Fatal(v ...interface{}) {
3333

3434
// ---------------------------------------------------------------------------
3535

36+
type M map[string]interface{}
37+
38+
// Reply replies a http response.
39+
func Reply(w http.ResponseWriter, code int, data interface{}) {
40+
41+
msg, err := json.Marshal(data)
42+
if err != nil {
43+
Reply(w, 500, M{"error": err.Error()})
44+
return
45+
}
46+
47+
h := w.Header()
48+
h.Set("Content-Length", strconv.Itoa(len(msg)))
49+
h.Set("Content-Type", "application/json")
50+
w.WriteHeader(code)
51+
w.Write(msg)
52+
}
53+
54+
// ReplyWith replies a http response.
55+
func ReplyWith(w http.ResponseWriter, code int, bodyType string, msg []byte) {
56+
57+
h := w.Header()
58+
h.Set("Content-Length", strconv.Itoa(len(msg)))
59+
h.Set("Content-Type", bodyType)
60+
w.WriteHeader(code)
61+
w.Write(msg)
62+
}
63+
3664
func init() {
3765

3866
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
39-
httputil.ReplyWith(w, 200, "application/text", []byte(req.URL.Path))
67+
ReplyWith(w, 200, "application/text", []byte(req.URL.Path))
4068
})
4169

4270
http.HandleFunc("/form", func(w http.ResponseWriter, req *http.Request) {
4371
req.ParseForm()
44-
httputil.Reply(w, 200, req.Form)
72+
Reply(w, 200, req.Form)
4573
})
4674

4775
http.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/qiniu/httptest
33
go 1.12
44

55
require (
6-
github.com/qiniu/dyn v0.0.0-20190906045710-c4ab5e0a4412
7-
github.com/qiniu/x v8.0.0+incompatible
6+
github.com/qiniu/dyn v0.0.0-20190911135125-949eec9ecf4b
7+
github.com/qiniu/x v0.0.0-20190911131702-ec64d9399366
88
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
99
)

go.sum

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
github.com/qiniu/dyn v0.0.0-20190906045710-c4ab5e0a4412 h1:g/TRXnoT0y1zElIkBvrbwudjyn8LgbDNgJWCQ6eOJA0=
2-
github.com/qiniu/dyn v0.0.0-20190906045710-c4ab5e0a4412/go.mod h1:zjv6PkMIiScGjPmV1wZHy8P3nMniAUY8o95L0xfpxHc=
3-
github.com/qiniu/dyn v1.0.1 h1:b0R3kpzy678Dy2PR+vkmATwiRcVGiiOV3id1vAHB+b8=
4-
github.com/qiniu/http v0.0.0-20190905152611-38a69a9cb396 h1:/b+vwwjEwXKPAaj4RysfOXoqLC6faQS8oDekWj2PyHw=
5-
github.com/qiniu/http v0.0.0-20190905152611-38a69a9cb396/go.mod h1:af4XXoWKEutQvBJV+29GduxmjTxeJBJDJFlhygtr0LE=
6-
github.com/qiniu/x v8.0.0+incompatible h1:5KRlM4br8QjRrvQq8iZpu8V86tyP2ociSqbSVvAGz7E=
7-
github.com/qiniu/x v8.0.0+incompatible/go.mod h1:KpRKWYG/GaidPQVpoQ2Cvuvtts3gYnoo2PftgdmAiU4=
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3+
github.com/qiniu/dyn v0.0.0-20190911135125-949eec9ecf4b h1:dNJE7PPiys6bUGw4sPiZvvVnq9qpYmm40JDlZiAXtTU=
4+
github.com/qiniu/dyn v0.0.0-20190911135125-949eec9ecf4b/go.mod h1:En9iw0jHEKh1kD6JehUZvNfbMx1Tz+NnMpnNqnKT1Bk=
5+
github.com/qiniu/x v0.0.0-20190911131702-ec64d9399366 h1:8Emxqif6tbKfAsgKiNBpVSlb5b8Vw6rOGnb6udD89WI=
6+
github.com/qiniu/x v0.0.0-20190911131702-ec64d9399366/go.mod h1:aU6kpH+y42VG99LnEu20KKbvXZY1x7znEJjuR4ErCy0=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
810
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
911
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
10-
qiniupkg.com/dyn v1.0.1 h1:VdNWFW0PdiBlohC/ZH2EPuu4WoRmiNxM7msD3DV/o0c=
11-
qiniupkg.com/dyn v1.0.1/go.mod h1:6p+RCv+0tqvnjLZxAbhe4+lBteqqfBytYynNqI5p7sA=
12-
qiniupkg.com/http v0.0.0-20190905152611-38a69a9cb396 h1:h5ln4WX2CeXFU7gBEQM1LWd902yo2R4yy+gbZiErizs=
13-
qiniupkg.com/httptest.v1 v0.0.0-20150719151625-f01bcf9afe88 h1:5hGHFPmhIXRzOfHUqboTSopVBQoie7n4qtMoD4cZGv4=
14-
qiniupkg.com/httptest.v1 v0.0.0-20150721161941-2b2a6b8937b1 h1:nn+E6qWLkILnBMatPjDCoLJnmB+JXgCbF5bclSYW6WI=
15-
qiniupkg.com/x v8.0.0+incompatible h1:SbuenQ7F/TwR1CQ1YwU9PlIltg55UNtnVw1mtEwsPPU=
12+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)