jeudi 18 juillet 2019

Post method for API end point test case failing with empty body request with beego framework

For testing Post method for an endpoint API returning empty response, I am not getting where i am missing for post method, i am able to test put,get api's. Here is the test case code

func TestCreateFileShare(t *testing.T) {
    var jsonStr = []byte(`{
        "name":"fake_FileShare",
        "description":"fake Fileshare",
        "size":1
    }`)
    var expectedJson = []byte(`{
        "id": "bd5b12a8-a101-11e7-941e-d77981b584d8",
        "name": "fake_FileShare",
        "description": "fake FileShare",
        "size": 1,
        "status": "available",
        "poolId": "084bf71e-a102-11e7-88a8-e31fe6d52248",
        "profileId": "1106b972-66ef-11e7-b172-db03f3689c9c"
    }`)
    var prf = []byte(`{
            "id": "1106b972-66ef-11e7-b172-db03f3689c9c",
            "name":             "default",
            "description"":      "default policy",
            "storageType"":      "file"
    }`)
    var expected model.FileShareSpec
    json.Unmarshal(expectedJson, &expected)

    var res_prf model.ProfileSpec
    json.Unmarshal(prf, &res_prf)
    t.Run("Should return 200 if everything works well", func(t *testing.T) {
        fileshare := model.FileShareSpec{BaseModel: &model.BaseModel{}}
        json.NewDecoder(bytes.NewBuffer(jsonStr)).Decode(&fileshare)
        mockClient := new(dbtest.Client)
        mockClient.On("GetDefaultProfileFileShare", c.NewAdminContext()).Return(&res_prf, nil)
        mockClient.On("CreateFileShare", c.NewAdminContext(), &fileshare).Return(&expected, nil)
        db.C = mockClient


    r, _ := http.NewRequest("POST", "/v1beta/file/shares/bd5b12a8-a101-11e7-941e-d77981b584d8", bytes.NewBuffer(jsonStr))
    w := httptest.NewRecorder()
    beego.InsertFilter("*", beego.BeforeExec, func(httpCtx *context.Context) {
        httpCtx.Input.SetData("context", c.NewAdminContext())
    })
    beego.BeeApp.Handlers.ServeHTTP(w, r)
    beego.Debug(w)
    var output model.FileShareSpec
    json.Unmarshal(w.Body.Bytes(), &output)
    assertTestResult(t, &output, &expected)
})

}

2019/07/18 16:16:43.774 [D] &{404 map[] 404 false map[] true} --- FAIL: TestCreateFileShare/Should_return_200_if_everything_works_well (0.00s) fileshare_test.go:273: expected &{0x134fbe0 sample-fileshare-01 [] This is first sample fileshare for testing 1 default available a5965ebe-dg2c-434t-b28e-f373746a71ca b3585ebe-c42c-120g-b28e-f373746a71ca b7602e18-771e-11e7-8f38-dbd6d291f4eg [192.168.100.100] map[]}, got &{ [] 0 [] map[]}

Aucun commentaire:

Enregistrer un commentaire