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

Commit

Permalink
github.com/qiniu/x v8.0.1; github.com/qiniu/dyn v2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 11, 2019
1 parent d1b7c04 commit 8ea0785
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
34 changes: 31 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package httptest

import (
"encoding/json"
"io"
"log"
"net/http"
"reflect"
"strconv"
"testing"

"github.com/qiniu/http/httputil"
"github.com/qiniu/x/mockhttp"
)

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

// ---------------------------------------------------------------------------

type M map[string]interface{}

// Reply replies a http response.
func Reply(w http.ResponseWriter, code int, data interface{}) {

msg, err := json.Marshal(data)
if err != nil {
Reply(w, 500, M{"error": err.Error()})
return
}

h := w.Header()
h.Set("Content-Length", strconv.Itoa(len(msg)))
h.Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(msg)
}

// ReplyWith replies a http response.
func ReplyWith(w http.ResponseWriter, code int, bodyType string, msg []byte) {

h := w.Header()
h.Set("Content-Length", strconv.Itoa(len(msg)))
h.Set("Content-Type", bodyType)
w.WriteHeader(code)
w.Write(msg)
}

func init() {

http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
httputil.ReplyWith(w, 200, "application/text", []byte(req.URL.Path))
ReplyWith(w, 200, "application/text", []byte(req.URL.Path))
})

http.HandleFunc("/form", func(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
httputil.Reply(w, 200, req.Form)
Reply(w, 200, req.Form)
})

http.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/qiniu/httptest
go 1.12

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

0 comments on commit 8ea0785

Please sign in to comment.