stuck on some request stuff

agolangf · · 452 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So, I&#39;m stuck on some request stuff. I&#39;m writing testing specifically - so while I&#39;m writing test cases for a handler, I&#39;m never actually starting up a server (and I don&#39;t think that should matter?)</p> <p>Just the same, it looks like request.ParseForm() is not reading the request body? I&#39;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&#39;ve swore this very thing works, cause I think I&#39;ve used similar other places - and everything looks kosher. I honestly think I&#39;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&#39;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&#39;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&#39;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&#39;t click to me that I couldn&#39;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&#39;s obvious now - it clearly says I cannot do what I&#39;m doing, I just missed it. I&#39;ll either change this to do ReadRequest, or I&#39;ll use this method - I&#39;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>&#39;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

452 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传