Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 3 additions & 46 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package dotweb

import (
"github.com/devfeel/dotweb/test"
"io"
"io/ioutil"
"net/http"
"strings"

"testing"
)

Expand All @@ -16,12 +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 @@ -232,7 +223,7 @@ func TestBinder_Bind_default(t *testing.T) {
t,
expected,
"",
toDefault,
test.ToDefault,
}

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

//init param
Expand All @@ -311,29 +302,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 @@ -375,14 +343,3 @@ func TestBinder_Bind_ContentTypeNull(t *testing.T) {
//check error must nil?
test.Nil(t, err)
}

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
}
167 changes: 167 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,168 @@
package dotweb

import (
"testing"
"github.com/devfeel/dotweb/test"
"encoding/json"
"fmt"
"net/http"
)

type Animal struct{
Hair string
HasMouth bool
}

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

//init param
context := initResponseContext(param)

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

animalJson,err:=json.Marshal(exceptedObject)
test.Nil(t,err)

//call function
status:=http.StatusNotFound
_,contextErr:=context.Write(status,animalJson)
test.Nil(t,contextErr)

//check result

//header
contentType:=context.response.header.Get(HeaderContentType)
//因writer中的header方法调用过http.Header默认设置
test.Equal(t,CharsetUTF8,contentType)
test.Equal(t,status,context.response.Status)

//body
body:=string(context.response.body)

test.Equal(t,string(animalJson),body)
}

//normal write string
//该用例是跑不过的- -,出来是一个[]uint8数组而不是string
func TestWriteString(t *testing.T) {
param := &InitContextParam{
t,
&Animal{},
"",
test.ToDefault,
}

//init param
context := initResponseContext(param)

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

animalJson,err:=json.Marshal(exceptedObject)
test.Nil(t,err)

//call function
_,contextErr:=context.WriteString(animalJson)
test.Nil(t,contextErr)

//header
contentType:=context.response.header.Get(HeaderContentType)
//因writer中的header方法调用过http.Header默认设置
test.Equal(t,CharsetUTF8,contentType)
test.Equal(t,defaultHttpCode,context.response.Status)

//body
body:=string(context.response.body)

//fmt.Printf("%T",context.response.body)

test.Equal(t,string(animalJson),body)
}

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

//init param
context := initResponseContext(param)

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

animalJson,err:=json.Marshal(exceptedObject)
test.Nil(t,err)

//call function
_,contextErr:=context.WriteJson(exceptedObject)
test.Nil(t,contextErr)

//header
contentType:=context.response.header.Get(HeaderContentType)
//因writer中的header方法调用过http.Header默认设置
test.Equal(t,MIMEApplicationJSONCharsetUTF8,contentType)
test.Equal(t,defaultHttpCode,context.response.Status)

//body
body:=string(context.response.body)

test.Equal(t,string(animalJson),body)
}

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

//init param
context := initResponseContext(param)

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

callback:="jsonCallBack"

//call function
_,err:=context.WriteJsonp(callback,exceptedObject)
test.Nil(t,err)

//check result

//header
contentType:=context.response.header.Get(HeaderContentType)
test.Equal(t,MIMEApplicationJavaScriptCharsetUTF8,contentType)
test.Equal(t,defaultHttpCode,context.response.Status)

//body
body:=string(context.response.body)

animalJson,err:=json.Marshal(exceptedObject)
test.Nil(t,err)

excepted:=fmt.Sprint(callback,"(",string(animalJson),");")

test.Equal(t,excepted,body)
}
5 changes: 5 additions & 0 deletions test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ func ToXML(t *testing.T, v interface{}) string {
//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
}