Can't get a test working with http GET parameters

blov · · 654 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve got a handler that has a GET url of &#34;/purchaseItems/{bidderID}/{eventID}&#34;</p> <p>I can&#39;t get a test to call the handler and pass in the parameters. It fails trying to parse the parameters to a number, saying it can&#39;t parse &#34;&#34; to an int.</p> <p>The test:</p> <pre><code>func TestPurchaseItems(t *testing.T) { db := testdatabase.TestDB{} handler := PurchaseItems(db) server := httptest.NewServer(handler) defer server.Close() url := server.URL + &#34;/purchaseItems/17/156&#34; resp, err := http.Get(url) if err != nil { t.Fatal(err) } if resp.StatusCode != 200 { t.Fatalf(&#34;Received non-200 response: %d\n&#34;, resp.StatusCode) } expected := fmt.Sprintf(&#34;Visitor count: %d.&#34;, 1) actual, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } if expected != string(actual) { t.Errorf(&#34;Expected the message &#39;%s&#39; but got &#39;%s&#39;\n&#34;, expected, string(actual)) } } </code></pre> <p>The test error:</p> <pre><code>http://127.0.0.1:57965/purchaseItems/17/156 ERRO[0000] strconv.ParseInt: parsing &#34;&#34;: invalid syntax --- FAIL: TestPurchaseItems (0.00s) purchaseItems_test.go:38: Expected the message &#39;Visitor count: 1.&#39; but got &#39;{ &#34;status&#34;: &#34;ERROR&#34;, &#34;message&#34;: &#34;We had some issue looking at the URL.&#34;, &#34;payload&#34;: null }&#39; FAIL exit status 1 FAIL mobilebid/controllers 0.012s </code></pre> <p>This means it isn&#39;t even parsing the parameters. Here&#39;s the handler registration code:</p> <pre><code> r.HandleFunc(&#34;/purchaseItems/{bidderID}/{eventID}&#34;, PurchaseItems(DB)).Methods(&#34;GET&#34;) </code></pre> <p>I don&#39;t think I need to use the params[&#34;bidderID&#34;] = []strings{} at all - it gives a different URL. </p> <p>How do I at least call this handler correctly to test it?</p> <hr/>**评论:**<br/><br/>pschlump: <pre><p>If you are using the Go library HandlerFunc it lacks the ability to process parameters in the URL. You might take a look at: <a href="http://www.gorillatoolkit.org/pkg/mux" rel="nofollow">http://www.gorillatoolkit.org/pkg/mux</a> -- It is the most popular mux -but slow - if you have a lot of URL paths. There are a bunch of them available. </p> <p>Also please provide the server side code so that a more meaningful answer can be provided.</p></pre>natdm: <pre><p>Here&#39;s my main go file:</p> <pre><code>package main import ( &#34;mobilebid/routes&#34; &#34;net/http&#34; _ &#34;net/http/pprof&#34; &#34;time&#34; &#34;github.com/joho/godotenv&#34; log &#34;github.com/Sirupsen/logrus&#34; &#34;github.com/gorilla/mux&#34; ) func main() { err := godotenv.Load() if err != nil { log.Fatal(&#34;Error loading .env file&#34;) } r := mux.NewRouter() r = routes.SetAuthenticatedRoutes(r) r = routes.SetUnauthenticatedRoutes(r) http.Handle(&#34;/&#34;, r) log.WithFields(log.Fields{ &#34;_timestamp&#34;: time.Now(), &#34;port&#34;: &#34;3000&#34;, }).Info(&#34;Starting server&#34;) log.Fatal(http.ListenAndServe(&#34;:3000&#34;, nil)) } </code></pre> <p>And the routes being resolved:</p> <pre><code>// SetUnauthenticatedRoutes sets all authenticated routes func SetUnauthenticatedRoutes(r *mux.Router) *mux.Router { r.HandleFunc(&#34;/purchaseItems/{bidderID}/{eventID}&#34;, PurchaseItems(DB)).Methods(&#34;GET&#34;) return r } </code></pre> <p>And the controller code for purchaseItems code (just the start, it&#39;s huge):</p> <pre><code>func PurchaseItems(dB db.AppDB) http.HandlerFunc { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { ps := mux.Vars(req) eventID, err := strconv.Atoi(ps[&#34;eventID&#34;]) bidderID, err := strconv.Atoi(ps[&#34;bidderID&#34;]) if err != nil { log.Error(err.Error()) res.Write(ResErr(errParsingURL.Error())) return } /*... rest of code... */ }) } </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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