Go – How to test with http.NewRequest

huangapple go评论78阅读模式
英文:

Go - How to test with http.NewRequest

问题

我有以下用于测试HTTP请求的代码:

func TestAuthenticate(t *testing.T) {
    api := &ApiResource{}
    ws := new(restful.WebService)
    ws.Consumes(restful.MIME_JSON, restful.MIME_XML)
    ws.Produces(restful.MIME_JSON, restful.MIME_JSON)
    ws.Route(ws.POST("/login").To(api.Authenticate))
    restful.Add(ws)

    bodyReader := strings.NewReader("<request><Username>42</Username><Password>adasddsa</Password><Channel>M</Channel></request>")

    httpRequest, _ := http.NewRequest("POST", "/login", bodyReader)
//  httpRequest.Header.Set("Content-Type", restful.MIME_JSON)
    httpRequest.Header.Set("Content-Type", restful.MIME_XML)
    httpWriter := httptest.NewRecorder()

    restful.DefaultContainer.ServeHTTP(httpWriter, httpRequest)
}

我尝试使用相同的NewReader将JSON作为字符串使用,还尝试使用json.Marshal使用结构体。

它们都不起作用。

有没有一种方法可以为http.NewRequest的有效第三个参数bodyReader编写代码?

NewReader中的JSON输入类似的请求是:

bodyReader := strings.NewReader("{'Username': '12124', 'Password': 'testinasg', 'Channel': 'M'}")

结构体字段为:Username, Password, Channel

英文:

I've got below code for testing http request:

func TestAuthenticate(t *testing.T) {
	api := &amp;ApiResource{}
	ws := new(restful.WebService)
	ws.Consumes(restful.MIME_JSON, restful.MIME_XML)
	ws.Produces(restful.MIME_JSON, restful.MIME_JSON)
	ws.Route(ws.POST(&quot;/login&quot;).To(api.Authenticate))
	restful.Add(ws)

    bodyReader := strings.NewReader(&quot;&lt;request&gt;&lt;Username&gt;42&lt;/Username&gt;&lt;Password&gt;adasddsa&lt;/Password&gt;&lt;Channel&gt;M&lt;/Channel&gt;&lt;/request&gt;&quot;)

	httpRequest, _ := http.NewRequest(&quot;POST&quot;, &quot;/login&quot;, bodyReader)
//	httpRequest.Header.Set(&quot;Content-Type&quot;, restful.MIME_JSON)
	httpRequest.Header.Set(&quot;Content-Type&quot;, restful.MIME_XML)
	httpWriter := httptest.NewRecorder()

	restful.DefaultContainer.ServeHTTP(httpWriter, httpRequest)
}

I tried to use json as a string with same NewReader and also tried to use struct with json.Marshal.

Neither of them works.

Is there a method where I can code bodyReader for a valid third parameter for http.NewRequest?

Similar request as input for NewReader in JSON is:

bodyReader := strings.NewReader(&quot;{&#39;Username&#39;: &#39;12124&#39;, &#39;Password&#39;: &#39;testinasg&#39;, &#39;Channel&#39;: &#39;M&#39;}&quot;)

Struct fields are is:
Username, Password, Channel

答案1

得分: 9

JSON是无效的。JSON使用&quot;来引用字符串,而不是&#39;

使用以下代码行创建请求体:

bodyReader := strings.NewReader(`{&quot;Username&quot;: &quot;12124&quot;, &quot;Password&quot;: &quot;testinasg&quot;, &quot;Channel&quot;: &quot;M&quot;}`)

我使用了原始字符串字面值来避免在JSON文本中引用&quot;

英文:

The JSON is invalid. JSON uses &quot; for quoting strings, not &#39;.

Use this line of code to create the request body:

bodyReader := strings.NewReader(`{&quot;Username&quot;: &quot;12124&quot;, &quot;Password&quot;: &quot;testinasg&quot;, &quot;Channel&quot;: &quot;M&quot;}`)

I used a raw string literal to avoid quoting the &quot; in the JSON text.

huangapple
  • 本文由 发表于 2014年10月12日 09:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/26320896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定