30 lines
809 B
Go
30 lines
809 B
Go
package searchapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"videoweb/pub/common/srvdef"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestSearchApiRejectsUnsupportedTypes(t *testing.T) {
|
|
for _, query := range []string{"type=1", "type=3", "type=2", "type=-1", "type=abc", "type="} {
|
|
writer := httptest.NewRecorder()
|
|
ctx, _ := gin.CreateTestContext(writer)
|
|
ctx.Request = httptest.NewRequest(http.MethodGet, "/search/?"+query, nil)
|
|
SearchApi(ctx)
|
|
var resp struct {
|
|
Code int64 `json:"code"`
|
|
}
|
|
if err := json.Unmarshal(writer.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("参数 %s 的响应无法解析: %v", query, err)
|
|
}
|
|
if resp.Code != int64(srvdef.ErrParamError) {
|
|
t.Errorf("参数 %s 返回 code %d, want %d", query, resp.Code, srvdef.ErrParamError)
|
|
}
|
|
}
|
|
}
|