Skip to content

Commit

Permalink
feat: add GetDownloadUrl handler Test
Browse files Browse the repository at this point in the history
  • Loading branch information
penqee committed Dec 2, 2024
1 parent bf8dfdc commit d72ffd6
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions api/handler/api/paper_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api

import (
"context"
"errors"
"github.com/bytedance/mockey"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/ut"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/cloudwego/hertz/pkg/route"
"github.com/stretchr/testify/assert"
"github.com/west2-online/fzuhelper-server/api/model/api"
"github.com/west2-online/fzuhelper-server/api/pack"
"github.com/west2-online/fzuhelper-server/api/rpc"
"github.com/west2-online/fzuhelper-server/kitex_gen/paper"
"net/http"
"testing"
)

func TestGetDownloadUrl(t *testing.T) {

Check failure on line 37 in api/handler/api/paper_service_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

type TestCase struct {
ExpectedError bool
ExpectedResult interface{}
FilePath string
}

testCases := []TestCase{
{
ExpectedError: false,
ExpectedResult: `{"code":"10000","message":"Success","data":{"url":"file://url"}}`,
FilePath: "url",
},
{
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) {

Check failure on line 60 in api/handler/api/paper_service_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

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)

if tc.ExpectedError {

Check failure on line 90 in api/handler/api/paper_service_test.go

View workflow job for this annotation

GitHub Actions / lint

dupBranchBody: both branches in if statement have same body (gocritic)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, tc.ExpectedResult, string(resp.Result().Body()))

} else {

Check failure on line 94 in api/handler/api/paper_service_test.go

View workflow job for this annotation

GitHub Actions / lint

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

}

Check failure on line 99 in api/handler/api/paper_service_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

}

Check failure on line 101 in api/handler/api/paper_service_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

0 comments on commit d72ffd6

Please sign in to comment.