2017-06-20 10 views
-4

golang web APIの単体テストを書いています。 私はdomain.com/api/user/currentのようなAPIを持っています。
このAPIはユーザーのログイン情報を返します。 私はhttp.NewRequestを使用してこのAPIをリクエストしています。しかし、それは動作しません。
goのための単体テストの記述方法Web api

どのようにこのAPIの自己呼び出しを許可でログに記録するのですか?

func TestGetCurrentUserLogged(t *testing.T) { 
    assert := assert.New(t) 

    var url = "http://example.com/user/current"; 
    req, _ := http.NewRequest(http.MethodGet, url, nil) 
    client := &http.Client{} 
    //req.Header.Add("Cookie", "")// 
    res, _ := client.Do(req) 
    if res.StatusCode == http.StatusBadGateway { 
     assert.True(false) 
    } 

    body, err := ioutil.ReadAll(res.Body) 
    defer res.Body.Close() 
    if err != nil { 
     t.Errorf("Error 1 API: %v", err.Error()) 
    } 
    var response map[string]interface{} 
    err = json.Unmarshal(body, &response) 
    if err != nil { 
     t.Errorf("Error 2 API:%v", err.Error()) 
    } 
} 
+2

「しかし動作しません」問題の説明ではありません。より具体的にしてください。 – Volker

+0

権限の問題がありますか? – RickyA

+0

これで、ログインサービスを呼び出してCookieを取得し、ヘッダーリクエストに追加しようとしました。 –

答えて

0

これは私が私のサーバーをテストしている...私は、これはより多くのあなたが上記のやりたいように見えるもののようであるより多くの機能テスト、であると信じるけれども。コードにコメントを追加して、何が起こっているのかを知ることができます。もちろん、これを取ってあなたのニーズに合わせて修正する必要があります。

type payload struct { 
    WhateverElse interface{} 
    Error  error 
} 

func TestSite(t *testing.T) { 
    ctx := context.Background() 
    ctx, cancel := context.WithCancel(ctx) 
    defer cancel() 
    // I'm using the Bone router in this example but can be replaced with most routers 
    myRouter := bone.New() 
    myRouter.GetFunc("/foo/:bar", routes.WipeCache) 
    myRouter.NotFoundFunc(routes.NotFound) 
    n := negroni.New() 
    n.UseHandler(wrappers.DefaultHeaders(myRouter)) 
    // start an instance of the server on port 8080 
    // to run different tests in different functions, change the port 
    srv := &http.Server{Addr: ":8080", Handler: n} 
    on := make(chan struct{}, 1) 
    go func(on chan struct{}) { 
     on <- struct{}{} 
     log.Fatal(srv.ListenAndServe()) 
    }(on) 
    // shutdown the server when i'm done 
    srv.Shutdown(ctx) 
    // the channel on is not really needed, but I like to have it to make sure srv.ListenAndServe() has had enough time to warm up before I make a requests. So i block just a tiny bit. 
    <-on 

    // create the client requests to test the server 
    client := &http.Client{ 
     Timeout: 1 * time.Second, 
    } 

    // Test logic 
    req, err := http.NewRequest("GET", "http://localhost:8080/foo/bar", nil) 
    if err != nil { 
     t.Errorf("An error occurred. %v", err) 
    } 
    resp, err := client.Do(req) 
    if err != nil { 
     t.Errorf("An error occurred. %v", err) 
    } 
    defer resp.Body.Close() 
    ctx.Done() 

    if status := resp.StatusCode; status != http.StatusOK { 
     t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusOK, status) 
    } 
    var data payload 
    body, err := ioutil.ReadAll(resp.Body) 
    if err != nil { 
     t.Errorf("An error occurred. %v", err) 
    } 
    json.Unmarshal(body, &data) 

    if data.Error != nil { 
     t.Errorf("An error occurred. %v", err) 
    } 
} 
関連する問題