|
| 1 | +package swagger |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + apiSpec "github.com/zeromicro/go-zero/tools/goctl/api/spec" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIsPostJson(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + method string |
| 15 | + hasJson bool |
| 16 | + expected bool |
| 17 | + }{ |
| 18 | + {"POST with JSON", http.MethodPost, true, true}, |
| 19 | + {"POST without JSON", http.MethodPost, false, false}, |
| 20 | + {"GET with JSON", http.MethodGet, true, false}, |
| 21 | + {"PUT with JSON", http.MethodPut, true, false}, |
| 22 | + } |
| 23 | + |
| 24 | + for _, tt := range tests { |
| 25 | + t.Run(tt.name, func(t *testing.T) { |
| 26 | + testStruct := createTestStruct("TestStruct", tt.hasJson) |
| 27 | + _, result := isPostJson(testingContext(t), tt.method, testStruct) |
| 28 | + assert.Equal(t, tt.expected, result) |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestParametersFromType(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + method string |
| 37 | + useDefinitions bool |
| 38 | + hasJson bool |
| 39 | + expectedCount int |
| 40 | + expectedBody bool |
| 41 | + }{ |
| 42 | + {"POST JSON with definitions", http.MethodPost, true, true, 1, true}, |
| 43 | + {"POST JSON without definitions", http.MethodPost, false, true, 1, true}, |
| 44 | + {"GET with form", http.MethodGet, false, false, 1, false}, |
| 45 | + {"POST with form", http.MethodPost, false, false, 1, false}, |
| 46 | + } |
| 47 | + |
| 48 | + for _, tt := range tests { |
| 49 | + t.Run(tt.name, func(t *testing.T) { |
| 50 | + ctx := Context{UseDefinitions: tt.useDefinitions} |
| 51 | + testStruct := createTestStruct("TestStruct", tt.hasJson) |
| 52 | + params := parametersFromType(ctx, tt.method, testStruct) |
| 53 | + |
| 54 | + assert.Equal(t, tt.expectedCount, len(params)) |
| 55 | + if tt.expectedBody { |
| 56 | + assert.Equal(t, paramsInBody, params[0].In) |
| 57 | + } else if len(params) > 0 { |
| 58 | + assert.NotEqual(t, paramsInBody, params[0].In) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestParametersFromType_EdgeCases(t *testing.T) { |
| 65 | + ctx := testingContext(t) |
| 66 | + |
| 67 | + params := parametersFromType(ctx, http.MethodPost, nil) |
| 68 | + assert.Empty(t, params) |
| 69 | + |
| 70 | + primitiveType := apiSpec.PrimitiveType{RawName: "string"} |
| 71 | + params = parametersFromType(ctx, http.MethodPost, primitiveType) |
| 72 | + assert.Empty(t, params) |
| 73 | +} |
| 74 | + |
| 75 | +func createTestStruct(name string, hasJson bool) apiSpec.DefineStruct { |
| 76 | + tag := `form:"username"` |
| 77 | + if hasJson { |
| 78 | + tag = `json:"username"` |
| 79 | + } |
| 80 | + |
| 81 | + return apiSpec.DefineStruct{ |
| 82 | + RawName: name, |
| 83 | + Members: []apiSpec.Member{ |
| 84 | + { |
| 85 | + Name: "Username", |
| 86 | + Type: apiSpec.PrimitiveType{RawName: "string"}, |
| 87 | + Tag: tag, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
0 commit comments