|
15 | 15 | package spec |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "bytes" |
| 19 | + "encoding/gob" |
18 | 20 | "encoding/json" |
19 | 21 | "testing" |
20 | 22 |
|
@@ -103,5 +105,100 @@ func TestSecurityProperty(t *testing.T) { |
103 | 105 | assert.Equal(t, securityContainsEmptyArray, props) |
104 | 106 | } |
105 | 107 | } |
| 108 | +} |
| 109 | + |
| 110 | +func TestOperationGobEncoding(t *testing.T) { |
| 111 | + // 1. empty scope in security requirements: "security": [ { "apiKey": [] } ], |
| 112 | + doTestOperationGobEncoding(t, operationJSON) |
| 113 | + |
| 114 | + // 2. nil security requirements |
| 115 | + doTestOperationGobEncoding(t, `{ |
| 116 | + "description": "operation description", |
| 117 | + "x-framework": "go-swagger", |
| 118 | + "consumes": [ "application/json", "application/x-yaml" ], |
| 119 | + "produces": [ "application/json", "application/x-yaml" ], |
| 120 | + "schemes": ["http", "https"], |
| 121 | + "tags": ["dogs"], |
| 122 | + "summary": "the summary of the operation", |
| 123 | + "operationId": "sendCat", |
| 124 | + "deprecated": true, |
| 125 | + "parameters": [{"$ref":"Cat"}], |
| 126 | + "responses": { |
| 127 | + "default": { |
| 128 | + "description": "void response" |
| 129 | + } |
| 130 | + } |
| 131 | +}`) |
106 | 132 |
|
| 133 | + // 3. empty security requirement |
| 134 | + doTestOperationGobEncoding(t, `{ |
| 135 | + "description": "operation description", |
| 136 | + "x-framework": "go-swagger", |
| 137 | + "consumes": [ "application/json", "application/x-yaml" ], |
| 138 | + "produces": [ "application/json", "application/x-yaml" ], |
| 139 | + "schemes": ["http", "https"], |
| 140 | + "tags": ["dogs"], |
| 141 | + "security": [], |
| 142 | + "summary": "the summary of the operation", |
| 143 | + "operationId": "sendCat", |
| 144 | + "deprecated": true, |
| 145 | + "parameters": [{"$ref":"Cat"}], |
| 146 | + "responses": { |
| 147 | + "default": { |
| 148 | + "description": "void response" |
| 149 | + } |
| 150 | + } |
| 151 | +}`) |
| 152 | + |
| 153 | + // 4. non-empty security requirements |
| 154 | + doTestOperationGobEncoding(t, `{ |
| 155 | + "description": "operation description", |
| 156 | + "x-framework": "go-swagger", |
| 157 | + "consumes": [ "application/json", "application/x-yaml" ], |
| 158 | + "produces": [ "application/json", "application/x-yaml" ], |
| 159 | + "schemes": ["http", "https"], |
| 160 | + "tags": ["dogs"], |
| 161 | + "summary": "the summary of the operation", |
| 162 | + "security": [ { "scoped-auth": [ "phone", "email" ] , "api-key": []} ], |
| 163 | + "operationId": "sendCat", |
| 164 | + "deprecated": true, |
| 165 | + "parameters": [{"$ref":"Cat"}], |
| 166 | + "responses": { |
| 167 | + "default": { |
| 168 | + "description": "void response" |
| 169 | + } |
| 170 | + } |
| 171 | +}`) |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | +func doTestOperationGobEncoding(t *testing.T, fixture string) { |
| 176 | + var src, dst Operation |
| 177 | + |
| 178 | + if !assert.NoError(t, json.Unmarshal([]byte(fixture), &src)) { |
| 179 | + t.FailNow() |
| 180 | + } |
| 181 | + |
| 182 | + doTestAnyGobEncoding(t, &src, &dst) |
| 183 | +} |
| 184 | + |
| 185 | +func doTestAnyGobEncoding(t *testing.T, src, dst interface{}) { |
| 186 | + expectedJSON, _ := json.MarshalIndent(src, "", " ") |
| 187 | + |
| 188 | + var b bytes.Buffer |
| 189 | + err := gob.NewEncoder(&b).Encode(src) |
| 190 | + if !assert.NoError(t, err) { |
| 191 | + t.FailNow() |
| 192 | + } |
| 193 | + |
| 194 | + err = gob.NewDecoder(&b).Decode(dst) |
| 195 | + if !assert.NoError(t, err) { |
| 196 | + t.FailNow() |
| 197 | + } |
| 198 | + |
| 199 | + jazon, err := json.MarshalIndent(dst, "", " ") |
| 200 | + if !assert.NoError(t, err) { |
| 201 | + t.FailNow() |
| 202 | + } |
| 203 | + assert.JSONEq(t, string(expectedJSON), string(jazon)) |
107 | 204 | } |
0 commit comments