本文整理汇总了Golang中net/http/httptest.NewRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRequest函数的具体用法?Golang NewRequest怎么用?Golang NewRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRequest函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTokenProxier
func TestTokenProxier(t *testing.T) {
a := New(t)
hdl := &testHandler{}
p := WithToken(hdl)
req := httptest.NewRequest("GET", "/uri", bytes.NewBuffer([]byte{}))
p.ServeHTTP(httptest.NewRecorder(), req)
a.So(hdl.req.Header.Get("Grpc-Metadata-Token"), ShouldBeEmpty)
req = httptest.NewRequest("GET", "/uri", bytes.NewBuffer([]byte{}))
req.Header.Add("Authorization", "Key blabla")
p.ServeHTTP(httptest.NewRecorder(), req)
a.So(hdl.req.Header.Get("Grpc-Metadata-Token"), ShouldBeEmpty)
req = httptest.NewRequest("GET", "/uri", bytes.NewBuffer([]byte{}))
req.Header.Add("Authorization", "bearer token")
p.ServeHTTP(httptest.NewRecorder(), req)
a.So(hdl.req.Header.Get("Grpc-Metadata-Token"), ShouldEqual, "token")
req = httptest.NewRequest("GET", "/uri", bytes.NewBuffer([]byte{}))
req.Header.Add("Authorization", "Bearer token")
p.ServeHTTP(httptest.NewRecorder(), req)
a.So(hdl.req.Header.Get("Grpc-Metadata-Token"), ShouldEqual, "token")
}
示例2: TestActionsPOST
// TestActionsPOST sample test for the POST call.
func TestActionsPOST(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need to test POST action call.")
{
action := "flagged_by"
userID := "ITEST_80aa936a-f618-4234-a7be-df59a14cf8de"
itemID := "ITEST_d16790f8-13e9-4cb4-b9ef-d82835589660"
url := fmt.Sprintf("/v1/action/%s/user/%s/on/item/%s", action, userID, itemID)
r := httptest.NewRequest("POST", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
t.Log("\tWhen we use version v1 of the actions endpoint.")
if w.Code != http.StatusOK {
t.Fatalf("\t%s\tShould be able to add the action : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to add the action.", tests.Success)
}
}
t.Log("Given the need to test a wrong POST action call.")
{
action := "flagged_by"
userID := "ITEST_80aa936a-f618-4234-a7be-df59a14cf8de"
itemID := "wrongitem"
url := fmt.Sprintf("/v1/action/%s/user/%s/on/item/%s", action, userID, itemID)
r := httptest.NewRequest("POST", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
t.Log("\tWhen we use version v1 of the actions endpoint.")
if w.Code != http.StatusInternalServerError {
t.Fatalf("\t%s\tShould fail on finding the target : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould fail on finding the target.", tests.Success)
}
}
}
示例3: TestMaskByName
// TestMaskByName tests the retrieval of a specific mask.
func TestMaskByName(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need to get a specific mask.")
{
url := "/v1/mask/" + mCollection + "/observation_time"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
if w.Code != 200 {
t.Fatalf("\t%s\tShould be able to retrieve the mask : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to retrieve the mask.", tests.Success)
var msk mask.Mask
if err := json.Unmarshal(w.Body.Bytes(), &msk); err != nil {
t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
if msk.Collection != mCollection && msk.Field != "observation_time" {
t.Fatalf("\t%s\tShould have the correct mask : %s - %s", tests.Failed, msk.Collection, msk.Field)
}
t.Logf("\t%s\tShould have the correct mask.", tests.Success)
}
}
}
示例4: TestReverseProxyInsecureSkipVerify
func TestReverseProxyInsecureSkipVerify(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stderr)
var requestReceived bool
backend := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestReceived = true
w.Write([]byte("Hello, client"))
}))
defer backend.Close()
// set up proxy
p := &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
Upstreams: []Upstream{newFakeUpstream(backend.URL, true)},
}
// create request and response recorder
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
p.ServeHTTP(w, r)
if !requestReceived {
t.Error("Even with insecure HTTPS, expected backend to receive request, but it didn't")
}
}
示例5: TestRetrieveRelationship
// TestRetrieveRelationship tests the retrieval of a specific relationship.
func TestRetrieveRelationship(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need to get a specific relationship.")
{
url := "/v1/relationship/" + relPrefix + "authored"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
if w.Code != 200 {
t.Fatalf("\t%s\tShould be able to retrieve the relationship : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to retrieve the relationship.", tests.Success)
var rel relationship.Relationship
if err := json.Unmarshal(w.Body.Bytes(), &rel); err != nil {
t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
if rel.Predicate != relPrefix+"authored" {
t.Fatalf("\t%s\tShould have the correct relationship : %s", tests.Failed, rel.Predicate)
}
t.Logf("\t%s\tShould have the correct relationship.", tests.Success)
}
}
}
示例6: TestAggregateGroup
func TestAggregateGroup(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need aggregate across a form's submissions.")
{
url := "/v1/form/580627b42600e2035218509f/aggregate/all"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling aggregate endpoint: %s", url)
{
t.Log("\tWhen we user version v1 of the aggregate endpoint.")
if w.Code != 200 {
t.Fatalf("\t%s\tShould be able to get the aggregation keys : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to get the aggregation keys .", tests.Success)
var ag form.Aggregation
if err := json.Unmarshal(w.Body.Bytes(), &ag); err != nil {
t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
if ag.Count != 9 {
t.Fatalf("\t%s\tShould have only one aggregation instead of %v.", tests.Failed, ag.Count)
}
t.Logf("\t%s\tShould have only one aggregation.", tests.Success)
}
}
}
示例7: TestDigest
// TestDigest tests the returning a form's digest
func TestDigest(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need aggregate across a form's submissions.")
{
url := "/v1/form/580627b42600e2035218509f/digest"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling aggregate endpoint: %s", url)
{
t.Log("\tWhen we user version v1 of the aggregate endpoint.")
if w.Code != 200 {
t.Fatalf("\t%s\tShould be able to get the aggregation keys : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to get the aggregation keys .", tests.Success)
var fm handlers.FormDigest
if err := json.Unmarshal(w.Body.Bytes(), &fm); err != nil {
t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
if len(fm.Questions) != 5 {
t.Fatalf("\t%s\tShould be able to return 1 question instead of %v.", tests.Failed, len(fm.Questions))
}
t.Logf("\t%s\tShould be able to return 1 question.", tests.Success)
}
}
}
示例8: TestWebSocketReverseProxyNonHijackerPanic
func TestWebSocketReverseProxyNonHijackerPanic(t *testing.T) {
// Capture the expected panic
defer func() {
r := recover()
if _, ok := r.(httpserver.NonHijackerError); !ok {
t.Error("not get the expected panic")
}
}()
var connCount int32
wsNop := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) { atomic.AddInt32(&connCount, 1) }))
defer wsNop.Close()
// Get proxy to use for the test
p := newWebSocketTestProxy(wsNop.URL)
// Create client request
r := httptest.NewRequest("GET", "/", nil)
r.Header = http.Header{
"Connection": {"Upgrade"},
"Upgrade": {"websocket"},
"Origin": {wsNop.URL},
"Sec-WebSocket-Key": {"x3JJHMbDL1EzLkh9GBhXDw=="},
"Sec-WebSocket-Version": {"13"},
}
nonHijacker := httptest.NewRecorder()
p.ServeHTTP(nonHijacker, r)
}
示例9: DELETERequest
func DELETERequest(path, accessToken string) *httptest.ResponseRecorder {
req := httptest.NewRequest(echo.DELETE, path, nil)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+accessToken)
res := httptest.NewRecorder()
e.ServeHTTP(res, req)
return res
}
示例10: TestHostHeaderReplacedUsingForward
func TestHostHeaderReplacedUsingForward(t *testing.T) {
var requestHost string
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestHost = r.Host
w.Write([]byte("Hello, client"))
}))
defer backend.Close()
upstream := newFakeUpstream(backend.URL, false)
proxyHostHeader := "test2.com"
upstream.host.UpstreamHeaders = http.Header{"Host": []string{proxyHostHeader}}
// set up proxy
p := &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
Upstreams: []Upstream{upstream},
}
r := httptest.NewRequest("GET", "/", nil)
r.Host = "test.com"
w := httptest.NewRecorder()
p.ServeHTTP(w, r)
if proxyHostHeader != requestHost {
t.Fatalf("Expected %s as a Host header got %s\n", proxyHostHeader, requestHost)
}
}
示例11: TestRetrievePattern
// TestRetrievePattern tests the retrieval of a specific pattern.
func TestRetrievePattern(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need to get a specific pattern.")
{
url := "/v1/pattern/" + patternPrefix + "comment"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
if w.Code != 200 {
t.Fatalf("\t%s\tShould be able to retrieve the pattern : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to retrieve the pattern.", tests.Success)
var p pattern.Pattern
if err := json.Unmarshal(w.Body.Bytes(), &p); err != nil {
t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
if p.Type != patternPrefix+"comment" {
t.Fatalf("\t%s\tShould have the correct pattern : %s", tests.Failed, p.Type)
}
t.Logf("\t%s\tShould have the correct pattern.", tests.Success)
}
}
}
示例12: TestActionsDELETE
// TestActionsDELETE sample test for the DELETE call.
func TestActionsDELETE(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need to test DELETE action call.")
{
action := "flagged_by"
userID := "ITEST_a63af637-58af-472b-98c7-f5c00743bac6"
itemID := "ITEST_d1dfa366-d2f7-4a4a-a64f-af89d4c97d82"
url := fmt.Sprintf("/v1/action/%s/user/%s/on/item/%s", action, userID, itemID)
r := httptest.NewRequest("DELETE", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
t.Log("\tWhen we use version v1 of the actions endpoint.")
if w.Code != http.StatusOK {
t.Fatalf("\t%s\tShould be able to remove the action : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to remove the action.", tests.Success)
}
}
}
示例13: TestExec
// TestExec tests the execution of a specific query.
func TestExec(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need to execute a specific query.")
{
url := "/v1/exec/" + qPrefix + "_basic?station_id=42021"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling url : %s", url)
{
if w.Code != http.StatusOK {
t.Fatalf("\t%s\tShould be able to retrieve the query : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to retrieve the query.", tests.Success)
recv := tests.IndentJSON(w.Body.String())
resp := tests.IndentJSON(`{"results":[{"Name":"Basic","Docs":[{"name":"C14 - Pasco County Buoy, FL"}]}]}`)
if resp != recv {
t.Log(resp)
t.Log(recv)
t.Fatalf("\t%s\tShould get the expected result.", tests.Failed)
}
t.Logf("\t%s\tShould get the expected result.", tests.Success)
}
}
}
示例14: TestAggregateGroupSubmission
func TestAggregateGroupSubmission(t *testing.T) {
tests.ResetLog()
defer tests.DisplayLog()
t.Log("Given the need aggregate across a form's submissions.")
{
url := "/v1/form/580627b42600e2035218509f/aggregate/d452b94d-e650-41c6-80af-c56091315c90/submission"
r := httptest.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
t.Logf("\tWhen calling aggregate endpoint: %s", url)
{
t.Log("\tWhen we user version v1 of the aggregate endpoint.")
if w.Code != 200 {
t.Fatalf("\t%s\tShould be able to get the aggregation keys : %v", tests.Failed, w.Code)
}
t.Logf("\t%s\tShould be able to get the aggregation keys .", tests.Success)
var ta []form.TextAggregation
if err := json.Unmarshal(w.Body.Bytes(), &ta); err != nil {
t.Fatalf("\t%s\tShould be able to unmarshal the results : %v", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to unmarshal the results.", tests.Success)
}
}
}
示例15: TestHostSimpleProxyNoHeaderForward
func TestHostSimpleProxyNoHeaderForward(t *testing.T) {
var requestHost string
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestHost = r.Host
w.Write([]byte("Hello, client"))
}))
defer backend.Close()
// set up proxy
p := &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
Upstreams: []Upstream{newFakeUpstream(backend.URL, false)},
}
r := httptest.NewRequest("GET", "/", nil)
r.Host = "test.com"
w := httptest.NewRecorder()
p.ServeHTTP(w, r)
if !strings.Contains(backend.URL, "//") {
t.Fatalf("The URL of the backend server doesn't contains //: %s", backend.URL)
}
expectedHost := strings.Split(backend.URL, "//")
if expectedHost[1] != requestHost {
t.Fatalf("Expected %s as a Host header got %s\n", expectedHost[1], requestHost)
}
}