84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package openlog
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestPrefixIncludesSideAndAction(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Set(CtxReqID, "req123")
|
|
SetScope(c, SideSource, ActionPush)
|
|
|
|
got := Prefix(c)
|
|
want := "[open.source_open_v1.push] req_id=req123 api_type=source_open_v1 action=push "
|
|
if got != want {
|
|
t.Fatalf("prefix mismatch: got %q want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestScopeFromPath(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
side string
|
|
method string
|
|
path string
|
|
wantSide string
|
|
wantAction string
|
|
wantType string
|
|
}{
|
|
{
|
|
name: "客户侧查询订单",
|
|
side: SideClient,
|
|
method: "GET",
|
|
path: "/api/client/v1/orders/FO123",
|
|
wantSide: SideClient,
|
|
wantAction: ActionQuery,
|
|
wantType: APITypeClientV1,
|
|
},
|
|
{
|
|
name: "客户侧创建订单",
|
|
side: SideClient,
|
|
method: "POST",
|
|
path: "/api/client/v1/orders",
|
|
wantSide: SideClient,
|
|
wantAction: ActionCreate,
|
|
wantType: APITypeClientV1,
|
|
},
|
|
{
|
|
name: "源头侧发货推送",
|
|
side: SideSource,
|
|
method: "POST",
|
|
path: "/api/open/v1/orders/ship-notify",
|
|
wantSide: SideSource,
|
|
wantAction: ActionPush,
|
|
wantType: APITypeSourceOpenV1,
|
|
},
|
|
{
|
|
name: "源头侧查询订单",
|
|
side: SideSource,
|
|
method: "GET",
|
|
path: "/api/open/v1/orders/FO123",
|
|
wantSide: SideSource,
|
|
wantAction: ActionQuery,
|
|
wantType: APITypeSourceOpenV1,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
gotSide, gotAction := ScopeFromPath(tc.side, tc.method, tc.path)
|
|
if gotSide != tc.wantSide || gotAction != tc.wantAction {
|
|
t.Fatalf("scope mismatch: got %s/%s want %s/%s", gotSide, gotAction, tc.wantSide, tc.wantAction)
|
|
}
|
|
if gotType := APITypeFromSide(gotSide); gotType != tc.wantType {
|
|
t.Fatalf("api type mismatch: got %s want %s", gotType, tc.wantType)
|
|
}
|
|
})
|
|
}
|
|
}
|