Go Appengine端点测试的问题

时间:2016-04-22 17:03:15

标签: google-app-engine go

我正在尝试使用appengine创建端点测试。不幸的是,由于在创建测试*Request结构时使用的url中缺少模式(和主机),测试仍然失败。在运行appengine测试时,会针对在半随机端口号上运行的特定测试生成服务器,这使得似乎无法定义完整的URL来执行测试。

像这样运行测试的official docs非常稀疏,只提供了一半的例子,所以我不知道如何让它工作。

这是我从代码段中的标记行获得的错误 Error: Received unexpected error "Post /auth: unsupported protocol scheme \"\""

测试代码

func TestEndpoints_Auth(t *testing.T) {

    // input data
    account := Account{
        AuthProvider:   "facebook",
        AuthProviderId: "123345456",
    }
    b, _ := json.Marshal(&account)
    reader := bytes.NewReader(b)

    // test server
    inst, err := aetest.NewInstance(nil)
    if !assert.NoError(t, err) { return }
    defer inst.Close()

    // request
    client := http.Client{}
    req, err := inst.NewRequest("POST", "/auth", reader)
    if !assert.NoError(t, err) { return }
    req.Header.Add(AppAuthToken, "foobar")
    resp, err := client.Do(req)
    if !assert.NoError(t, err) { return }   // <=== Where the error occurs

    // tests
    if !assert.Nil(t, err) { return }
    assert.Equal(t, http.StatusCreated, resp.StatusCode)
}

日志 [GIN-debug] POST /auth --> bitbucket.org/chrisolsen/chriscamp.(*endpoints).Auth-fm (5 handlers) [GIN-debug] GET /accounts/me --> bitbucket.org/chrisolsen/chriscamp.(*endpoints).GetMyAccount-fm (7 handlers) INFO 2016-04-22 13:23:39,278 devappserver2.py:769] Skipping SDK update check. WARNING 2016-04-22 13:23:39,278 devappserver2.py:785] DEFAULT_VERSION_HOSTNAME will not be set correctly with --port=0 WARNING 2016-04-22 13:23:39,345 simple_search_stub.py:1126] Could not read search indexes from c:\users\chris\appdata\local\temp\appengine.testapp\search_indexes INFO 2016-04-22 13:23:39,354 api_server.py:205] Starting API server at: http://localhost:54461 INFO 2016-04-22 13:23:41,043 dispatcher.py:197] Starting module "default" running at: http://localhost:54462 INFO 2016-04-22 13:23:41,046 admin_server.py:116] Starting admin server at: http://localhost:54466

1 个答案:

答案 0 :(得分:0)

我真的希望执行api blackbox测试,但似乎可以通过appengine进行撤销。相反,我现在直接在端点上执行测试。

req, _ := inst.NewRequest("POST", "/auth", reader)
req.Header.Add(AppAuthToken, "foobar")
resp := httptest.NewRecorder()

handlePostAuth(resp, req)
assert.Equal(t, http.StatusCreated, resp.Code)