Skip to content

Commit

Permalink
context.go测试用例.
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyixian committed Jun 15, 2017
1 parent 059772a commit bd0e97d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 63 deletions.
67 changes: 4 additions & 63 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ package dotweb
import (
"testing"
"github.com/devfeel/dotweb/test"
"net/http"
"encoding/json"
"io/ioutil"
"strings"
"io"
"encoding/xml"
"testing"
)

type Person struct {
Expand All @@ -18,13 +13,6 @@ type Person struct {
Legs []string
}

type InitContextParam struct {
t *testing.T
v interface{}
contentType string
convertHandler func(t *testing.T,v interface{})string
}

//json
func TestBinder_Bind_json(t *testing.T) {

Expand Down Expand Up @@ -238,7 +226,7 @@ func TestBinder_Bind_default(t *testing.T) {
t,
expected,
"",
toDefault,
test.ToDefault,
}

//init param
Expand Down Expand Up @@ -295,7 +283,7 @@ func TestBinder_Bind_default_error(t *testing.T) {
t,
expected,
"application/xml",
toDefault,
test.ToDefault,
}

//init param
Expand All @@ -319,30 +307,6 @@ func TestBinder_Bind_default_error(t *testing.T) {

}

//common init context
func initContext(param *InitContextParam) *HttpContext {
httpRequest:=&http.Request{}
context:=&HttpContext{
request:&Request{
Request:httpRequest,
},
}
header:=make(map[string][]string)
header["Accept-Encoding"]=[]string{"gzip, deflate"}
header["Accept-Language"]=[]string{"en-us"}
header["Foo"]=[]string{"Bar", "two"}
//specify json
header["Content-Type"]=[]string{param.contentType}
context.request.Header=header

jsonStr:=param.convertHandler(param.t,param.v)
body:=format(jsonStr)
context.request.Request.Body=body


return context
}

//default
//TODO:content type is null but body not null,is it right??
func TestBinder_Bind_ContentTypeNull(t *testing.T) {
Expand Down Expand Up @@ -383,28 +347,5 @@ func TestBinder_Bind_ContentTypeNull(t *testing.T) {
err:=binder.Bind(person,context)

//check error must nil?
test.Nil(t,err)
}

func toJson(t *testing.T,v interface{}) string{
b,err:=json.Marshal(v)
test.Nil(t,err)
return string(b)
}
func toXml(t *testing.T,v interface{}) string{
b,err:=xml.Marshal(v)
test.Nil(t,err)
//t.Log("xml:",string(b))
return string(b)
}

func toDefault(t *testing.T,v interface{}) string{
return ""
}

func format(b string) io.ReadCloser{
s := strings.NewReader(b)
r := ioutil.NopCloser(s)
r.Close()
return r
test.Nil(t, err)
}
31 changes: 31 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
package dotweb

import (
"testing"
"github.com/devfeel/dotweb/test"
)

type Animal struct{
Hair string
HasMouth bool
}

func TestWriteJsonpBlob(t *testing.T) {
param := &InitContextParam{
t,
&Animal{},
"",
test.ToDefault,
}

//init param
context := initResponseContext(param)

excepted:=&Animal{
"Black",
true,
}

context.WriteJsonp("jsonCallBack",excepted)

t.Log(string(context.response.body))
}
27 changes: 27 additions & 0 deletions test/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test

import (
"encoding/json"
"encoding/xml"
"testing"
)

// ToJson
func ToJson(t *testing.T, v interface{}) string {
b, err := json.Marshal(v)
Nil(t, err)
return string(b)
}

// ToXML
func ToXML(t *testing.T, v interface{}) string {
b, err := xml.Marshal(v)
Nil(t, err)
//t.Log("xml:",string(b))
return string(b)
}

//ToDefault
func ToDefault(t *testing.T, v interface{}) string {
return ""
}
82 changes: 82 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package dotweb

import (
"net/http"
"testing"
"io"
"strings"
"io/ioutil"
"fmt"
"bytes"
)

//common init context
func initContext(param *InitContextParam) *HttpContext {
httpRequest := &http.Request{}
context := &HttpContext{
request: &Request{
Request: httpRequest,
},
}
header := make(map[string][]string)
header["Accept-Encoding"] = []string{"gzip, deflate"}
header["Accept-Language"] = []string{"en-us"}
header["Foo"] = []string{"Bar", "two"}
//specify json
header["Content-Type"] = []string{param.contentType}
context.request.Header = header

jsonStr := param.convertHandler(param.t, param.v)
body := format(jsonStr)
context.request.Request.Body = body

return context
}

//init response context
func initResponseContext(param *InitContextParam) *HttpContext {
context := &HttpContext{
response: &Response{
},
}

var buf1 bytes.Buffer
w := io.MultiWriter(&buf1)

writer := &gzipResponseWriter{
ResponseWriter:&httpWriter{},
Writer:w,
}

context.response=NewResponse(writer)

return context
}
type httpWriter http.Header

func (ho httpWriter) Header() http.Header {
return http.Header(ho)
}

func (ho httpWriter) Write(byte []byte) (int, error) {
fmt.Println("string:",string(byte))
return 0,nil
}

func (ho httpWriter) WriteHeader(code int) {
fmt.Println("code:",code)
}

func format(b string) io.ReadCloser {
s := strings.NewReader(b)
r := ioutil.NopCloser(s)
r.Close()
return r
}

type InitContextParam struct {
t *testing.T
v interface{}
contentType string
convertHandler func(t *testing.T, v interface{}) string
}

0 comments on commit bd0e97d

Please sign in to comment.