doujie7886 2016-10-29 06:56
浏览 117
已采纳

golang appengine api测试错误“ appengine:NewContext传递了未知的http.Request”

I'm using appengine and golang to develop simple RESTful APIs. The code works fine when I start service using goapp serve, and I started writing the unit test functions to test the API endpoints, and I'm struck here with the panic error appengine: NewContext passed an unknown http.Request. I'm getting this error when i run goapp test.

It looks like for some reason, i'm not able to pass the request that I created and pass it to appengine.NewContext()

Below is the snippet of the code..

body := strings.NewReader("")

request, err := http.NewRequest("GET", "endpoint url", body) //inst.NewRequest("GET", goalUrl, body) //
if err != nil {
    t.Error(err)
}
t.Log(request)

c := appengine.NewContext(request) // ERROR: appengine: NewContext passed an unknown http.Request

I have created a simple reproducible code. Can you help me with this? or Does anyone have their golang API project on appengine, and have unit test functions to test the endpoints, i'll like to take a look at their code...

Here is the gitlab issue that I posted, which has all the required details of the issue along with examples and detailed error message. Thank for all your help.

  • 写回答

2条回答 默认 最新

  • dongzi1397 2016-11-09 23:48
    关注

    Apologies for the delay in response. I was working on the solution myself

    Here is how I solved the go appengine unit tests issue: the complete solution code can be found on this github repo branch.

    In the *_test.go file, I have used the following.

    • httptest.NewServer(..) to create a new instance of the test server. also used to capture the base url of the service which is used in preparing the request object.
    • aetest.NewContext() to create a new context for testing purpose
    • http.NewRequest(..) to create the new request
    • gorilla's context.Set(..) to assign key ("Context"), value (context created in above step) to the request created above
    • httptest.NewRecorder() a new recorder to save the results
    • http.Handler.ServeHTTP(..) passing the recorded and request. used to make the API request

    For each API handler code, instead of directly creating the new context using appengine.NewContext, I have code described in the below sudocode

    using the gorilla's context.GetOk(..), check 
        IF the received request object has the key "Context"
        THEN using value of that key as the context
        ELSE derive context using appengine.NewContext(r)
    

    I have wrapped the reusable functionality into a separate library called aeunittest and used it in my code.

    With this setup, i'm, able to run the goapp test to fire the unit tests. see the solved code for complete details of the solution.

    Here is the blogpost that helped me find this solution. Thanks a lot Mark. TESTING GO HTTP HANDLERS IN GOOGLE APP ENGINE WITH MUX AND HIGHER ORDER FUNCTIONS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛