本文整理匯總了Golang中github.com/drewolson/testflight.WithServer函數的典型用法代碼示例。如果您正苦於以下問題:Golang WithServer函數的具體用法?Golang WithServer怎麽用?Golang WithServer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了WithServer函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestPing
func TestPing(t *testing.T) {
testflight.WithServer(handler, func(r *testflight.Requester) {
response := r.Get("/ping")
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, "OK", response.Body)
})
}
示例2: TestCommonHeaders
func TestCommonHeaders(t *testing.T) {
corsOrigins := []string{"http://example.com"}
Convey("Test common headers", t, func() {
testflight.WithServer(
testHandler(corsOrigins),
func(r *testflight.Requester) {
headers := r.Get("/ping").RawResponse.Header
Convey("Sets content-type header", func() {
actual := headers.Get("Content-Type")
So(actual, ShouldEqual, "application/vnd.api+json")
})
Convey("Sets access-control-allow header", func() {
actual := headers.Get("Access-Control-Allow-Headers")
So(actual, ShouldEqual, "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
})
},
)
})
Convey("Test CORS headers", t, func() {
testflight.WithServer(
testHandler(corsOrigins),
func(r *testflight.Requester) {
headerName := "Access-Control-Allow-Origin"
request, _ := http.NewRequest(
"GET", "/ping", strings.NewReader(""),
)
Convey("Returns CORS header for allowed origin", func() {
request.Header.Add("Origin", "http://example.com")
actual := r.Do(request).RawResponse.Header.Get(headerName)
So(actual, ShouldEqual, "http://example.com")
})
Convey("Does not return CORS header for not allowed origin", func() {
request.Header.Add("Origin", "http://not-allowed.com")
actual := r.Do(request).RawResponse.Header.Get(headerName)
So(actual, ShouldEqual, "")
})
},
)
})
}
示例3: TestUserGet
func TestUserGet(t *testing.T) {
testflight.WithServer(rooter(goji.DefaultMux), func(r *testflight.Requester) {
req, _ := http.NewRequest("GET", "/user/edit/22", nil)
req.Header.Set("Authorization", "Basic dXNlcjp1c2Vy")
response := r.Do(req)
assert.Equal(t, 200, response.StatusCode)
})
}
示例4: TestWebSocketRecordsReceivedMessages
func TestWebSocketRecordsReceivedMessages(t *testing.T) {
testflight.WithServer(websocketHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.WriteMessage("Drew")
connection.ReceiveMessage()
assert.Equal(t, "Hello, Drew", connection.ReceivedMessages[0])
})
}
示例5: TestWebSocketTimesOutWhileFlushingMessages
func TestWebSocketTimesOutWhileFlushingMessages(t *testing.T) {
testflight.WithServer(donothingwebsocketHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.WriteMessage("Drew")
err := connection.FlushMessages(2)
assert.Equal(t, TimeoutError{}, *err)
})
}
示例6: TestWebSocket
func TestWebSocket(t *testing.T) {
testflight.WithServer(websocketHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.SendMessage("Drew")
message, _ := connection.ReceiveMessage()
assert.Equal(t, "Hello, Drew", message)
})
}
示例7: testDelete
func testDelete(t *testing.T, url string) {
testflight.WithServer(
CRUDRouter(),
func(r *testflight.Requester) {
response := r.Delete(url, testflight.JSON, "")
structJSONCompare(t, http.StatusNoContent, response.StatusCode)
},
)
}
示例8: TestWebSocketFlushesMessages
func TestWebSocketFlushesMessages(t *testing.T) {
testflight.WithServer(multiresponsewebsocketHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.WriteMessage("Drew")
connection.WriteMessage("Bob")
connection.FlushMessages(2)
assert.Equal(t, 2, len(connection.ReceivedMessages))
})
}
示例9: TestWebSocketReceiveMessageTimesOut
func TestWebSocketReceiveMessageTimesOut(t *testing.T) {
testflight.WithServer(donothingwebsocketHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.WriteMessage("Drew")
_, err := connection.ReceiveMessage()
assert.Equal(t, TimeoutError{}, *err)
})
}
示例10: TestClosingConnections
func TestClosingConnections(t *testing.T) {
testflight.WithServer(pollingHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.SendMessage("Drew")
connection.SendMessage("Bob")
connection.FlushMessages(2)
connection.Close()
assert.Equal(t, 2, len(connection.ReceivedMessages))
})
}
示例11: TestWebSocketTimeoutIsConfigurable
func TestWebSocketTimeoutIsConfigurable(t *testing.T) {
testflight.WithServer(websocketHandler(), func(r *testflight.Requester) {
connection := Connect(r, "/websocket")
connection.Timeout = 2 * time.Second
go func() {
time.Sleep(1 * time.Second)
connection.WriteMessage("Drew")
}()
message, _ := connection.ReceiveMessage()
assert.Equal(t, "Hello, Drew", message)
})
}
示例12: TestProducer
func TestProducer(t *testing.T) {
stack := new(mango.Stack)
handler := stack.HandlerFunc(pact.Producer)
testflight.WithServer(handler, func(r *testflight.Requester) {
pact_str, err := ioutil.ReadFile("../pacts/my_consumer-my_producer.json")
if err != nil {
t.Error(err)
}
pacts := make(map[string]interface{})
err = json.Unmarshal(pact_str, &pacts)
if err != nil {
t.Error(err)
}
for _, i := range pacts["interactions"].([]interface{}) {
interaction := i.(map[string]interface{})
t.Logf("Given %s", interaction["producer_state"])
t.Logf(" %s", interaction["description"])
request := interaction["request"].(map[string]interface{})
var actualResponse *testflight.Response
switch request["method"] {
case "get":
actualResponse = r.Get(request["path"].(string) + "?" + request["query"].(string))
}
expectedResponse := interaction["response"].(map[string]interface{})
assert.Equal(t, int(expectedResponse["status"].(float64)), actualResponse.StatusCode)
for k, v := range expectedResponse["headers"].(map[string]interface{}) {
assert.Equal(t, v, actualResponse.RawResponse.Header[k][0])
}
responseBody := make(map[string]interface{})
err = json.Unmarshal([]byte(actualResponse.Body), &responseBody)
if err != nil {
t.Error(err)
}
for _, diff := range pretty.Diff(expectedResponse["body"], responseBody) {
t.Log(diff)
}
assert.Equal(t, expectedResponse["body"], responseBody)
}
})
}
示例13: TestUserDelete
func TestUserDelete(t *testing.T) {
testflight.WithServer(rooter(goji.DefaultMux), func(r *testflight.Requester) {
Users := []models.User{}
count_before := 0
count_after := 0
db.Find(&Users).Count(&count_before)
req, _ := http.NewRequest("GET", "/user/delete/1", nil)
req.Header.Set("Authorization", "Basic dXNlcjp1c2Vy")
r.Do(req)
db.Find(&Users).Count(&count_after)
assert.Equal(t, count_before-1, count_after)
})
}
示例14: testRead
func testRead(t *testing.T, url string, v interface{}) {
testflight.WithServer(
CRUDRouter(),
func(r *testflight.Requester) {
response := r.Get(url)
structJSONCompare(t, http.StatusOK, response.StatusCode)
err := json.Unmarshal([]byte(response.Body), v)
if err != nil {
t.Error(err)
}
},
)
}
示例15: TestUserCreateError
func TestUserCreateError(t *testing.T) {
testflight.WithServer(rooter(goji.DefaultMux), func(r *testflight.Requester) {
count_before := 0
count_after := 0
db.Table("users").Count(&count_before)
values := url.Values{}
values.Add("Name", "エラー")
req, _ := http.NewRequest("POST", "/user/new", strings.NewReader(values.Encode()))
req.Header.Set("Authorization", "Basic dXNlcjp1c2Vy")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response := r.Do(req)
db.Table("users").Count(&count_after)
assert.Equal(t, 200, response.StatusCode)
assert.Equal(t, count_before, count_after)
})
}