forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zouyixian
committed
Jun 15, 2017
1 parent
059772a
commit bd0e97d
Showing
4 changed files
with
144 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |