<p>So, I'm stuck on some request stuff. I'm writing testing specifically - so while I'm writing test cases for a handler, I'm never actually starting up a server (and I don't think that should matter?)</p>
<p>Just the same, it looks like request.ParseForm() is not reading the request body? I've tried to boil it down as simply as I can here:</p>
<p><a href="http://play.golang.org/p/hGWDz6GtDR" rel="nofollow">http://play.golang.org/p/hGWDz6GtDR</a></p>
<p>Any insight/help is appreciated. :-) I could've swore this very thing works, cause I think I've used similar other places - and everything looks kosher. I honestly think I'm just stuck at this point and need a jostle to get me unstuck.</p>
<hr/>**评论:**<br/><br/>dsymonds: <pre><p>From the net/http.NewRequest docs:</p>
<blockquote>
<p>NewRequest returns a Request suitable for use with Client.Do or
Transport.RoundTrip. To create a request for use with testing a Server
Handler use either ReadRequest or manually update the Request fields.
See the Request type's documentation for the difference between inbound
and outbound request fields.</p>
</blockquote></pre>jahayhurst: <pre><p>That is helpful - having that bit put in front of me without the other stuff. I read that sentence from the documentation probably 5 times, I've poured through that package quite a bit, but quoting that relevant bit does clear it up quite a bit.</p>
<p>The way I had read the package (apparently without fully focusing on it) made me think I could test the handler individually like this, and it looks like I cannot - not like this. I was going to put my muxing in the same file eventually anyway, but I can test the handlers through the muxing and the server and it shouldn't be that bad. I guess in the end I needed a kick to push me that way :-)</p>
<p>All the same, ty tons :-)</p></pre>dsymonds: <pre><p>The net/http/httptest package can make writing tests easier.</p></pre>jahayhurst: <pre><p>I had saw that, and was using httptest.ResponseRecorder to inspect the response from the handler. It just didn't click to me that I couldn't call the handler directly - even though I read it quite a few times.</p></pre>daveddev: <pre><p>I hope this helps; Here is how I might test a handler and nested handler which should each set a response header:</p>
<p><a href="http://play.golang.org/p/rhnHPYXwzq" rel="nofollow">http://play.golang.org/p/rhnHPYXwzq</a></p>
<p>There are some minor differences from what would be in an actual Go test file, but it should be simple enough to make the changes.</p>
<p>edit: provided a more thorough and correct play</p></pre>jahayhurst: <pre><p>That does help.</p>
<p>I was trying to avoid setting up a server, and manually test the handler with a request - mostly so my unit test was just testing that specific bit.</p>
<p>But the quote from above pointed something out to me that's obvious now - it clearly says I cannot do what I'm doing, I just missed it. I'll either change this to do ReadRequest, or I'll use this method - I'm not sure which way is going to look simpler. I might write a test for each way, who knows?</p>
<p>Still, I appreciate the help :-)</p></pre>elithrar_: <pre><p><a href="/u/daved" rel="nofollow">/u/daved</a>'s way is definitely the simplest method. Create a Request, create a ResponseRecorder, and pass them both to handler.ServeHTTP. </p></pre>quiI: <pre><p>This might help: <a href="https://github.com/quii/testing-gorillas" rel="nofollow">https://github.com/quii/testing-gorillas</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传