| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package spec |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/stretchr/testify/assert" |
| 22 |
"github.com/stretchr/testify/require" |
| 23 |
jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting" |
| 24 |
) |
| 25 |
|
| 26 |
var operation = Operation{ |
| 27 |
VendorExtensible: VendorExtensible{ |
| 28 |
Extensions: map[string]interface{}{ |
| 29 |
"x-framework": "go-swagger", |
| 30 |
}, |
| 31 |
}, |
| 32 |
OperationProps: OperationProps{ |
| 33 |
Description: "operation description", |
| 34 |
Consumes: []string{"application/json", "application/x-yaml"}, |
| 35 |
Produces: []string{"application/json", "application/x-yaml"}, |
| 36 |
Schemes: []string{"http", "https"}, |
| 37 |
Tags: []string{"dogs"}, |
| 38 |
Summary: "the summary of the operation", |
| 39 |
ID: "sendCat", |
| 40 |
Deprecated: true, |
| 41 |
Security: []map[string][]string{ |
| 42 |
{ |
| 43 |
"apiKey": {}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
Parameters: []Parameter{ |
| 47 |
{Refable: Refable{Ref: MustCreateRef("Cat")}}, |
| 48 |
}, |
| 49 |
Responses: &Responses{ |
| 50 |
ResponsesProps: ResponsesProps{ |
| 51 |
Default: &Response{ |
| 52 |
ResponseProps: ResponseProps{ |
| 53 |
Description: "void response", |
| 54 |
}, |
| 55 |
}, |
| 56 |
}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
const operationJSON = `{ |
| 62 |
"description": "operation description", |
| 63 |
"x-framework": "go-swagger", |
| 64 |
"consumes": [ "application/json", "application/x-yaml" ], |
| 65 |
"produces": [ "application/json", "application/x-yaml" ], |
| 66 |
"schemes": ["http", "https"], |
| 67 |
"tags": ["dogs"], |
| 68 |
"summary": "the summary of the operation", |
| 69 |
"operationId": "sendCat", |
| 70 |
"deprecated": true, |
| 71 |
"security": [ { "apiKey": [] } ], |
| 72 |
"parameters": [{"$ref":"Cat"}], |
| 73 |
"responses": { |
| 74 |
"default": { |
| 75 |
"description": "void response" |
| 76 |
} |
| 77 |
} |
| 78 |
}` |
| 79 |
|
| 80 |
func TestIntegrationOperation(t *testing.T) { |
| 81 |
var actual Operation |
| 82 |
if assert.NoError(t, json.Unmarshal([]byte(operationJSON), &actual)) { |
| 83 |
assert.EqualValues(t, actual, operation) |
| 84 |
} |
| 85 |
|
| 86 |
assertParsesJSON(t, operationJSON, operation) |
| 87 |
} |
| 88 |
|
| 89 |
func TestOperationRoundtrip(t *testing.T) { |
| 90 |
cases := []jsontesting.RoundTripTestCase{ |
| 91 |
{ |
| 92 |
|
| 93 |
|
| 94 |
Name: "UnmarshalEmbedded", |
| 95 |
JSON: `{ |
| 96 |
"description": "a cool description", |
| 97 |
"x-framework": "swagger-go" |
| 98 |
}`, |
| 99 |
Object: &Operation{ |
| 100 |
VendorExtensible{Extensions{ |
| 101 |
"x-framework": "swagger-go", |
| 102 |
}}, |
| 103 |
OperationProps{ |
| 104 |
Description: "a cool description", |
| 105 |
}, |
| 106 |
}, |
| 107 |
}, { |
| 108 |
Name: "BasicCase", |
| 109 |
JSON: operationJSON, |
| 110 |
Object: &operation, |
| 111 |
}, |
| 112 |
} |
| 113 |
|
| 114 |
for _, tcase := range cases { |
| 115 |
t.Run(tcase.Name, func(t *testing.T) { |
| 116 |
require.NoError(t, tcase.RoundTripTest(&Operation{})) |
| 117 |
}) |
| 118 |
} |
| 119 |
} |