Skip to content

Commit

Permalink
context.go中writeJson测试用例.
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyixian committed Jun 15, 2017
1 parent 5786752 commit ba9db5a
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,81 @@ func TestWrite(t *testing.T) {
//body
body:=string(context.response.body)

test.Equal(t,body,string(animalJson))
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
Expand Down Expand Up @@ -90,5 +164,5 @@ func TestWriteJsonp(t *testing.T) {

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

test.Equal(t,body,excepted)
test.Equal(t,excepted,body)
}

0 comments on commit ba9db5a

Please sign in to comment.