@@ -18,6 +18,7 @@ package api
1818
1919import (
2020 "context"
21+ "strings"
2122 "testing"
2223
2324 "github.com/bytedance/mockey"
@@ -591,3 +592,56 @@ func TestGetTermsList(t *testing.T) {
591592 })
592593 }
593594}
595+
596+ func TestGetSignedLocationApiUrl (t * testing.T ) {
597+ type testCase struct {
598+ name string
599+ mockSignedURL string
600+ mockHeaders map [string ]string
601+ mockErr error
602+ body * ut.Body
603+ expectContains string
604+ }
605+
606+ validBody := `{"location":"119.262647,26.106131"}`
607+
608+ testCases := []testCase {
609+ {
610+ name : "success" ,
611+ mockSignedURL : "https://restapi.amap.com/v3/place/around?key=xxx&scode=abc" ,
612+ mockHeaders : map [string ]string {"User-Agent" : "AMAP_Location_SDK_Android" },
613+ body : & ut.Body {Body : strings .NewReader (validBody ), Len : len (validBody )},
614+ expectContains : `{"code":"10000","message":"Success","data":` ,
615+ },
616+ {
617+ name : "rpc error" ,
618+ mockErr : errno .InternalServiceError ,
619+ body : & ut.Body {Body : strings .NewReader (validBody ), Len : len (validBody )},
620+ expectContains : `{"code":"50001","message":"内部服务错误"` ,
621+ },
622+ {
623+ name : "bind error" ,
624+ body : nil ,
625+ expectContains : `{"code":"20001","message":"参数错误` ,
626+ },
627+ }
628+
629+ router := route .NewEngine (& config.Options {})
630+ router .POST ("/api/v1/common/signed-location-api-url" , GetSignedLocationApiUrl )
631+
632+ defer mockey .UnPatchAll ()
633+ for _ , tc := range testCases {
634+ mockey .PatchConvey (tc .name , t , func () {
635+ if tc .name != "bind error" {
636+ mockey .Mock (rpc .GetSignedLocationApiUrlRPC ).To (func (ctx context.Context , req * common.GetSignedLocationApiUrlRequest ) (string , map [string ]string , error ) {
637+ return tc .mockSignedURL , tc .mockHeaders , tc .mockErr
638+ }).Build ()
639+ }
640+
641+ res := ut .PerformRequest (router , consts .MethodPost , "/api/v1/common/signed-location-api-url" , tc .body ,
642+ ut.Header {Key : "Content-Type" , Value : "application/json" })
643+ assert .Equal (t , consts .StatusOK , res .Result ().StatusCode ())
644+ assert .Contains (t , string (res .Result ().Body ()), tc .expectContains )
645+ })
646+ }
647+ }
0 commit comments