Skip to content

Commit

Permalink
test: modified GetDownloadUrl handler Test
Browse files Browse the repository at this point in the history
  • Loading branch information
penqee committed Dec 3, 2024
1 parent 739f047 commit 6330bbe
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions api/handler/api/paper_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,60 @@ import (

func TestGetDownloadUrl(t *testing.T) {
type TestCase struct {
Name string
ExpectedError bool
ExpectedResult interface{}
FilePath string
}

testCases := []TestCase{
{
Name: "GetUrlSuccessfully",
ExpectedError: false,
ExpectedResult: `{"code":"10000","message":"Success","data":{"url":"file://url"}}`,
FilePath: "url",
},
{
Name: "GetUrlFailed",
ExpectedError: true,
ExpectedResult: `{"code":"50001","message":"GetDownloadUrlRPC: RPC called failed: wrong filepath"}`,
FilePath: "",
},
}

router := route.NewEngine(&config.Options{})

router.GET("/api/v1/paper/download", func(ctx context.Context, c *app.RequestContext) {
filepath := c.DefaultQuery("filepath", "/")
mockey.Mock(rpc.GetDownloadUrlRPC).To(func(ctx context.Context, req *paper.GetDownloadUrlRequest) (url string, err error) {
// 因为handler不能重复注册,无法添加一个TestError(bool)来作为错误的判断,只能先用filepath为空暂代了
if filepath == "" {
return "", errors.New("GetDownloadUrlRPC: RPC called failed: wrong filepath")
}
return "file://" + req.Filepath, nil
}).Build()
defer mockey.UnPatchAll()

url, err := rpc.GetDownloadUrlRPC(ctx, &paper.GetDownloadUrlRequest{
Filepath: filepath,
})
if err != nil {
pack.RespError(c, err)
return
}

resp := new(api.GetDownloadUrlResponse)
resp.URL = url

pack.RespData(c, resp)
})
for _, tc := range testCases {
url := "/api/v1/paper/download" + "?filepath=" + tc.FilePath

resp := ut.PerformRequest(router, consts.MethodGet, url, nil)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, tc.ExpectedResult, string(resp.Result().Body()))
mockey.PatchConvey(tc.Name, t, func() {
router := route.NewEngine(&config.Options{})
router.GET("/api/v1/paper/download", func(ctx context.Context, c *app.RequestContext) {
filepath := c.DefaultQuery("filepath", "/")
mockey.Mock(rpc.GetDownloadUrlRPC).To(func(ctx context.Context, req *paper.GetDownloadUrlRequest) (url string, err error) {
if tc.ExpectedError {
return "", errors.New("GetDownloadUrlRPC: RPC called failed: wrong filepath")
}
return "file://" + req.Filepath, nil
}).Build()
defer mockey.UnPatchAll()

url, err := rpc.GetDownloadUrlRPC(ctx, &paper.GetDownloadUrlRequest{
Filepath: filepath,
})
if err != nil {
pack.RespError(c, err)
return
}

resp := new(api.GetDownloadUrlResponse)
resp.URL = url

pack.RespData(c, resp)
})

url := "/api/v1/paper/download" + "?filepath=" + tc.FilePath

resp := ut.PerformRequest(router, consts.MethodGet, url, nil)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, tc.ExpectedResult, string(resp.Result().Body()))
})
}
}

0 comments on commit 6330bbe

Please sign in to comment.