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

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

golang appengine api testing error "appengine: NewContext passed an unknown http.Request"

问题

我正在使用appengine和golang开发简单的RESTful API。当我使用goapp serve启动服务时,代码运行正常,我开始编写单元测试函数来测试API端点,但是我在这里遇到了恐慌错误appengine: NewContext passed an unknown http.Request。当我运行goapp test时,我会得到这个错误。

看起来由于某种原因,我无法将我创建的请求传递给appengine.NewContext()

以下是代码片段:

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

我创建了一个简单的可复现代码。你能帮我解决这个问题吗?或者有没有人在appengine上有他们的golang API项目,并且有单元测试函数来测试端点,我想看看他们的代码...

这是我发布的gitlab问题,其中包含了问题的所有必要细节,包括示例和详细的错误消息。感谢你的帮助。

英文:

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.

答案1

得分: 2

抱歉回复延迟了。我正在自己解决问题。

以下是我解决go appengine单元测试问题的方法:完整的解决方案代码可以在这个github仓库的分支中找到。

在*_test.go文件中,我使用了以下内容:

  • 使用httptest.NewServer(..)创建一个新的测试服务器实例,也用于捕获服务的基本URL,该URL用于准备请求对象。
  • 使用aetest.NewContext()创建一个新的测试上下文
  • 使用http.NewRequest(..)创建新的请求
  • 使用gorilla的context.Set(..)将键("Context")和值(上述步骤中创建的上下文)分配给上述创建的请求
  • 使用httptest.NewRecorder()创建一个新的记录器来保存结果
  • 使用http.Handler.ServeHTTP(..)传递记录器和请求,用于发起API请求

对于每个API处理程序的代码,我没有直接使用appengine.NewContext来创建新的上下文,而是使用了下面伪代码中描述的代码:

使用gorilla的context.GetOk(..)检查
    IF 接收到的请求对象具有键"Context"
    THEN 使用该键的值作为上下文
    ELSE 使用appengine.NewContext(r)来派生上下文

我将可重用的功能封装到一个名为aeunittest的单独库中,并在我的代码中使用它。

通过这个设置,我能够运行goapp test来执行单元测试。有关解决方案的完整详细信息,请参阅解决的代码

这是帮助我找到这个解决方案的博文。非常感谢Mark。在Google App Engine中使用MUX和高阶函数测试Go HTTP处理程序

英文:

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.

答案2

得分: 1

NewContextflight HTTP请求中派生出一个上下文,即已经向其注册的请求。

由于您正在创建一个新的请求,该请求对于内部appengine包来说是未知的,因此它会出现恐慌。

该包还提供了一个RegisterTestRequest函数,但在主要文档中没有提到,所以可能会有所不同。我对此没有经验。

英文:

NewContext derives a context from in flight HTTP requests, i.e. ones that have been registered with it.

Since you are creating a new request, unknown to the internal appengine package, it's panicking.

That package also provides a RegisterTestRequest function, but it's not mentioned in the main documentation so YMMV. I have no experience with this.

huangapple
  • 本文由 发表于 2016年10月29日 14:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/40316822.html
匿名

发表评论

匿名网友

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

确定