本文整理汇总了Golang中github.com/golib/assert.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Test_Context_RedirectWithAbort
func Test_Context_RedirectWithAbort(t *testing.T) {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/path/to/resource?key=url_value&test=url_true", nil)
location := "https://www.example.com"
assertion := assert.New(t)
ctx := NewContext(newMockServer())
ctx.Request = request
ctx.Response = &Response{
ResponseWriter: recorder,
}
ctx.handlers = []Middleware{
func(ctx *Context) {
ctx.Redirect(location)
ctx.Next()
},
func(ctx *Context) {
ctx.Render(NewDefaultRender(ctx.Response), "next render")
},
}
ctx.Next()
assertion.Equal(location, recorder.Header().Get("Location"))
assertion.NotContains(recorder.Body.String(), "next render")
}
开发者ID:dolab,项目名称:gogo,代码行数:26,代码来源:context_test.go
示例2: Test_RouteMockHandle
func Test_RouteMockHandle(t *testing.T) {
server := newMockServer()
route := NewAppRoute("/", server)
response := httptest.NewRecorder()
assertion := assert.New(t)
// mock handler
route.MockHandle("GET", "/mock", response, func(ctx *Context) {
ctx.Text("MOCK")
})
// start server
ts := httptest.NewServer(server)
defer ts.Close()
// testing by http request
request, _ := http.NewRequest("GET", ts.URL+"/mock", nil)
res, err := http.DefaultClient.Do(request)
assertion.Nil(err)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assertion.Nil(err)
assertion.Empty(body)
assertion.Equal(http.StatusOK, response.Code)
assertion.Equal("MOCK", response.Body.String())
}
开发者ID:dolab,项目名称:gogo,代码行数:29,代码来源:route_test.go
示例3: Test_MultipartRequest
func Test_MultipartRequest(t *testing.T) {
assertion := assert.New(t)
method := "put"
uri := "/put/multipart"
filename := "gopher.png"
file := filepath.Clean("./fixtures/gopher.png")
params := map[string]string{"form_key": "form_value"}
server := newMockServer(method, uri, func(w http.ResponseWriter, r *http.Request) {
assertion.Equal(method, r.Method)
assertion.Equal("/put/multipart", r.RequestURI)
assertion.Equal("form_value", r.FormValue("form_key"))
freader, fheader, ferr := r.FormFile("filename")
assertion.Nil(ferr)
assertion.Equal(filename, fheader.Filename)
fb, _ := ioutil.ReadAll(freader)
b, _ := ioutil.ReadFile(file)
assertion.Equal(b, fb)
w.Header().Set("x-request-method", r.Method)
w.WriteHeader(http.StatusNoContent)
w.Write([]byte(""))
})
ts := httptest.NewServer(server)
defer ts.Close()
client := New(ts.URL, false)
client.NewMultipartRequest(t, method, uri, "gopher.png", file, params)
client.AssertStatus(http.StatusNoContent)
client.AssertHeader("x-request-method", method)
client.AssertEmpty()
}
开发者ID:dolab,项目名称:httptesting,代码行数:34,代码来源:client_test.go
示例4: Test_NewJsonResponder
func Test_NewJsonResponder(t *testing.T) {
assertion := assert.New(t)
code := 200
header := http.Header{
"Content-Type": []string{"text/plain"},
"X-Testing": []string{"testing"},
}
body := struct {
Name string `json:"name"`
}{"testing"}
rawurl := "https://example.com"
rawbody := `{"name":"testing"}`
responder := NewJsonResponder(code, header, body)
request, _ := http.NewRequest("GET", rawurl, nil)
response, err := responder.RoundTrip(request)
assertion.Nil(err)
assertion.Equal(code, response.StatusCode)
assertion.Equal("application/json", response.Header.Get("Content-Type"))
assertion.Equal(strconv.Itoa(len(rawbody)), response.Header.Get("Content-Length"))
b, _ := ioutil.ReadAll(response.Body)
response.Body.Close()
assertion.Equal(rawbody, string(b))
}
开发者ID:dolab,项目名称:httpmitm,代码行数:26,代码来源:responder_test.go
示例5: Test_PutForm
func Test_PutForm(t *testing.T) {
assertion := assert.New(t)
method := "PUT"
uri := "/put?key"
params := url.Values{"form_key": []string{"form_value"}}
server := newMockServer(method, uri, func(w http.ResponseWriter, r *http.Request) {
assertion.Equal(method, r.Method)
assertion.Equal(uri, r.RequestURI)
assertion.Equal("application/x-www-form-urlencoded", r.Header.Get("Content-Type"))
assertion.Equal("form_value", r.FormValue("form_key"))
_, ok := r.URL.Query()["key"]
assertion.True(ok)
assertion.Empty(r.FormValue("key"))
w.Header().Set("x-request-method", r.Method)
w.WriteHeader(http.StatusOK)
w.Write([]byte(method + " " + uri + " OK!"))
})
ts := httptest.NewServer(server)
defer ts.Close()
client := New(ts.URL, false)
client.PutForm(t, uri, params)
client.AssertOK()
client.AssertHeader("x-request-method", method)
client.AssertContains("PUT /put?key OK!")
}
开发者ID:dolab,项目名称:httptesting,代码行数:29,代码来源:client_helpers_test.go
示例6: Test_NewXmlResponderWithError
func Test_NewXmlResponderWithError(t *testing.T) {
assertion := assert.New(t)
code := 200
header := http.Header{
"Content-Type": []string{"text/plain"},
"X-Testing": []string{"testing"},
}
body := struct {
XMLName xml.Name
Ch chan<- bool `xml:"Channel"`
}{
XMLName: xml.Name{
Space: "http://xmlns.example.com",
Local: "Responder",
},
Ch: make(chan<- bool, 1),
}
rawurl := "https://exmaple.com"
responder := NewXmlResponder(code, header, body)
request, _ := http.NewRequest("GET", rawurl, nil)
response, err := responder.RoundTrip(request)
assertion.NotNil(err)
assertion.Nil(response)
}
开发者ID:dolab,项目名称:httpmitm,代码行数:26,代码来源:responder_test.go
示例7: Test_NewResponder
func Test_NewResponder(t *testing.T) {
assertion := assert.New(t)
code := 200
header := http.Header{
"Content-Type": []string{"text/plain"},
"X-Testing": []string{"testing"},
}
body := "Hello, world!"
rawurl := "https://example.com"
responder := NewResponder(code, header, body)
assertion.Implements((*http.RoundTripper)(nil), responder)
request, _ := http.NewRequest("GET", rawurl, nil)
response, err := responder.RoundTrip(request)
assertion.Nil(err)
assertion.Equal(code, response.StatusCode)
assertion.Equal(strconv.Itoa(len(body)), response.Header.Get("Content-Length"))
assertion.NotNil(response.Request)
b, err := ioutil.ReadAll(response.Body)
response.Body.Close()
assertion.Nil(err)
assertion.Equal(body, string(b))
}
开发者ID:dolab,项目名称:httpmitm,代码行数:25,代码来源:responder_test.go
示例8: Test_NewXmlResponder
func Test_NewXmlResponder(t *testing.T) {
assertion := assert.New(t)
code := 200
header := http.Header{
"Content-Type": []string{"text/plain"},
"X-Testing": []string{"testing"},
}
body := struct {
XMLName xml.Name
Name string `xml:"Name"`
}{
XMLName: xml.Name{
Space: "http://xmlns.example.com",
Local: "Responder",
},
Name: "testing",
}
rawurl := "https://exmpale.com"
rawbody := `<Responder xmlns="http://xmlns.example.com"><Name>testing</Name></Responder>`
responder := NewXmlResponder(code, header, body)
request, _ := http.NewRequest("GET", rawurl, nil)
response, err := responder.RoundTrip(request)
assertion.Nil(err)
assertion.Equal(code, response.StatusCode)
assertion.Equal("text/xml", response.Header.Get("Content-Type"))
assertion.Equal(strconv.Itoa(len(rawbody)), response.Header.Get("Content-Length"))
b, _ := ioutil.ReadAll(response.Body)
response.Body.Close()
assertion.Equal(rawbody, string(b))
}
开发者ID:dolab,项目名称:httpmitm,代码行数:33,代码来源:responder_test.go
示例9: Test_FilterRequest
func Test_FilterRequest(t *testing.T) {
assertion := assert.New(t)
method := "GET"
uri := "/request/filter"
server := newMockServer(method, uri, func(w http.ResponseWriter, r *http.Request) {
assertion.Equal(method, r.Method)
assertion.Equal("/request/filter", r.RequestURI)
assertion.Equal("request filter", r.Header.Get("X-Filtered-Header"))
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
})
ts := httptest.NewServer(server)
defer ts.Close()
request, _ := http.NewRequest(method, ts.URL+"/request/filter", nil)
client := New(ts.URL, false)
client.NewFilterRequest(t, request, func(r *http.Request) error {
r.Header.Add("X-Filtered-Header", "request filter")
return nil
})
client.AssertOK()
client.AssertEmpty()
}
开发者ID:dolab,项目名称:httptesting,代码行数:27,代码来源:client_test.go
示例10: Test_NewAppServer
func Test_NewAppServer(t *testing.T) {
assertion := assert.New(t)
server := newMockServer()
assertion.Implements((*http.Handler)(nil), server)
assertion.IsType(&Context{}, server.pool.Get())
}
开发者ID:dolab,项目名称:gogo,代码行数:7,代码来源:server_test.go
示例11: Test_Context_RequestHeader
func Test_Context_RequestHeader(t *testing.T) {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "https://www.example.com/resource?key=url_value&test=url_true", nil)
request.Header.Add("X-Canonical-Key", "Canonical-Value")
request.Header["x-normal-key"] = []string{"normal value"}
params := NewAppParams(request, httprouter.Params{})
assertion := assert.New(t)
server := newMockServer()
ctx := server.new(recorder, request, params, nil)
assertion.True(ctx.HasRawHeader("X-Canonical-Key"))
assertion.False(ctx.HasRawHeader("x-canonical-key"))
assertion.True(ctx.HasHeader("X-Canonical-Key"))
assertion.True(ctx.HasHeader("x-canonical-key"))
assertion.True(ctx.HasRawHeader("x-normal-key"))
assertion.False(ctx.HasRawHeader("X-Normal-Key"))
assertion.False(ctx.HasHeader("x-normal-key"))
assertion.False(ctx.HasHeader("X-Normal-Key"))
assertion.Equal("Canonical-Value", ctx.RawHeader("X-Canonical-Key"))
assertion.Empty(ctx.RawHeader("x-canonical-key"))
assertion.Equal("Canonical-Value", ctx.Header("X-Canonical-Key"))
assertion.Equal("Canonical-Value", ctx.Header("x-canonical-key"))
assertion.Equal("normal value", ctx.RawHeader("x-normal-key"))
assertion.Empty(ctx.RawHeader("X-Normal-Key"))
assertion.Empty(ctx.Header("x-normal-key"))
assertion.Empty(ctx.Header("X-Normal-Key"))
}
开发者ID:dolab,项目名称:gogo,代码行数:27,代码来源:context_test.go
示例12: Test_MitmTransportPauseAndResume
func Test_MitmTransportPauseAndResume(t *testing.T) {
assertion := assert.New(t)
mt := NewMitmTransport()
mt.StubDefaultTransport(t)
defer mt.UnstubDefaultTransport()
// mocks
mt.MockRequest("GET", "https://example.com/").WithResponse(101, nil, "GET OK").AnyTimes()
// response with mocked
response, err := http.Get("mitm://example.com")
assertion.Nil(err)
assertion.Equal(101, response.StatusCode)
// paused and response with real github server
mt.Pause()
response, err = http.Get("mitm://example.com")
assertion.Nil(err)
assertion.Equal(200, response.StatusCode)
// resume and response with mock again
mt.Resume()
response, err = http.Get("mitm://example.com")
assertion.Nil(err)
assertion.Equal(101, response.StatusCode)
}
开发者ID:dolab,项目名称:httpmitm,代码行数:29,代码来源:transport_test.go
示例13: Test_AppParamsXml
func Test_AppParamsXml(t *testing.T) {
str := `<Params>
<Key>xml_value</Key>
<Test>true</Test>
</Params>`
request, _ := http.NewRequest("POST", "/path/to/resource?key=url_value&test=url_true", strings.NewReader(str))
request.Header.Set("Content-Type", "application/json")
routeParams := httprouter.Params{
httprouter.Param{"test", "route_true"},
}
assertion := assert.New(t)
p := NewAppParams(request, routeParams)
var params struct {
XMLName xml.Name `xml:"Params"`
Key string `xml:"Key"`
Test bool `xml:"Test"`
}
err := p.Xml(¶ms)
assertion.Nil(err)
assertion.Equal("xml_value", params.Key)
assertion.True(params.Test)
}
开发者ID:dolab,项目名称:gogo,代码行数:25,代码来源:param_test.go
示例14: Test_MitmTransportWithTimesError
func Test_MitmTransportWithTimesError(t *testing.T) {
mt := NewMitmTransport().StubDefaultTransport(t)
defer mt.UnstubDefaultTransport()
assertion := assert.New(t)
type result struct {
Code int `json:"code"`
Name string `json:"name"`
}
// mocks
mt.MockRequest("GET", "http://example.com").WithJsonResponse(200, nil, result{
Code: 200,
Name: "OK",
}).Times(3)
// GET mitm://example.com
for i := 0; i < 3; i++ {
response, err := http.Get("mitm://example.com")
assertion.Nil(err)
assertion.Equal(200, response.StatusCode)
b, err := ioutil.ReadAll(response.Body)
response.Body.Close()
assertion.Nil(err)
assertion.Equal(`{"code":200,"name":"OK"}`, string(b))
}
}
开发者ID:dolab,项目名称:httpmitm,代码行数:30,代码来源:transport_test.go
示例15: Test_AppParamsGob
func Test_AppParamsGob(t *testing.T) {
type data struct {
Key string `bson:"key"`
Test bool `bson:"test"`
}
var buf bytes.Buffer
assertion := assert.New(t)
params := data{
Key: "gob_value",
Test: true,
}
err := gob.NewEncoder(&buf).Encode(params)
assertion.Nil(err)
request, _ := http.NewRequest("POST", "/path/to/resource?key=url_value&test=url_true", strings.NewReader(buf.String()))
request.Header.Set("Content-Type", "application/gob")
p := NewAppParams(request, httprouter.Params{})
var temp data
err = p.Gob(&temp)
assertion.Nil(err)
assertion.Equal("gob_value", temp.Key)
assertion.True(temp.Test)
}
开发者ID:dolab,项目名称:gogo,代码行数:28,代码来源:param_test.go
示例16: Test_NewObjectId
func Test_NewObjectId(t *testing.T) {
assertion := assert.New(t)
// Generate 10 ids
ids := make([]ObjectId, 10)
for i := 0; i < 10; i++ {
ids[i] = NewObjectId()
}
for i := 1; i < 10; i++ {
id := ids[i]
prevId := ids[i-1]
// Test for uniqueness among all other 9 generated ids
for j, tid := range ids {
if j != i {
assertion.NotEqual(id, tid)
}
}
// Check that timestamp was incremented and is within 30 seconds of the previous one
assertion.InDelta(prevId.Time().Second(), id.Time().Second(), 0.1)
// Check that machine ids are the same
assertion.Equal(prevId.Machine(), id.Machine())
// Check that pids are the same
assertion.Equal(prevId.Pid(), id.Pid())
// Test for proper increment
assertion.Equal(1, int(id.Counter()-prevId.Counter()))
}
}
开发者ID:dolab,项目名称:gogo,代码行数:33,代码来源:utils_test.go
示例17: Test_ConfigSection
func Test_ConfigSection(t *testing.T) {
assertion := assert.New(t)
config, _ := newMockConfig("application.json")
section := config.Section()
assertion.NotNil(section.Server)
assertion.NotNil(section.Logger)
}
开发者ID:dolab,项目名称:gogo,代码行数:8,代码来源:config_test.go
示例18: Test_NewConfigWithoutName
func Test_NewConfigWithoutName(t *testing.T) {
assertion := assert.New(t)
config, _ := NewStringAppConfig(`{
"mode": "test"
}`)
assertion.Equal("GOGO", config.Name)
}
开发者ID:dolab,项目名称:gogo,代码行数:8,代码来源:config_test.go
示例19: Test_NewConfigWithoutMode
func Test_NewConfigWithoutMode(t *testing.T) {
assertion := assert.New(t)
config, _ := NewStringAppConfig(`{
"name": "testing"
}`)
assertion.Equal(Development, config.Mode)
}
开发者ID:dolab,项目名称:gogo,代码行数:8,代码来源:config_test.go
示例20: Test_NewResponse
func Test_NewResponse(t *testing.T) {
recorder := httptest.NewRecorder()
assertion := assert.New(t)
response := NewResponse(recorder)
assertion.Implements((*Responser)(nil), response)
assertion.Equal(http.StatusOK, response.Status())
assertion.Equal(noneHeaderFlushed, response.Size())
}
开发者ID:dolab,项目名称:gogo,代码行数:9,代码来源:response_test.go
注:本文中的github.com/golib/assert.New函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论