1// Copyright 2015 go-swagger maintainers 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package analysis 16 17import ( 18 "path/filepath" 19 "strconv" 20 "testing" 21 22 "github.com/go-openapi/spec" 23 "github.com/stretchr/testify/assert" 24) 25 26func Test_FixEmptyResponseDescriptions(t *testing.T) { 27 bp := filepath.Join("fixtures", "fixer", "fixer.yaml") 28 sp := loadOrFail(t, bp) 29 FixEmptyResponseDescriptions(sp) 30 31 for path, pathItem := range sp.Paths.Paths { 32 if !assert.NotNil(t, pathItem, "expected a fixture with all path items provided in: %s", path) { 33 continue 34 } 35 36 if path == "/noDesc" { 37 // scope for fixed descriptions 38 assertAllVerbs(t, pathItem, true) 39 } else { 40 // scope for unchanged descriptions 41 assertAllVerbs(t, pathItem, false) 42 } 43 } 44 for r, resp := range sp.Responses { 45 pin := resp 46 assert.Truef(t, assertResponse(t, "/responses/"+r, &pin, true), 47 "expected a fixed empty description in response %s", r) 48 } 49} 50 51func assertAllVerbs(t *testing.T, pathItem spec.PathItem, isEmpty bool) { 52 msg := "expected %s description for %s" 53 var mode string 54 if isEmpty { 55 mode = "a fixed empty" 56 } else { 57 mode = "an unmodified" 58 } 59 assert.Truef(t, assertResponseInOperation(t, pathItem.Get, isEmpty), msg, mode, "GET") 60 assert.Truef(t, assertResponseInOperation(t, pathItem.Put, isEmpty), msg, mode, "PUT") 61 assert.Truef(t, assertResponseInOperation(t, pathItem.Post, isEmpty), msg, mode, "POST") 62 assert.Truef(t, assertResponseInOperation(t, pathItem.Delete, isEmpty), msg, mode, "DELETE") 63 assert.Truef(t, assertResponseInOperation(t, pathItem.Options, isEmpty), msg, mode, "OPTIONS") 64 assert.Truef(t, assertResponseInOperation(t, pathItem.Patch, isEmpty), msg, mode, "PATCH") 65 assert.Truef(t, assertResponseInOperation(t, pathItem.Head, isEmpty), msg, mode, "HEAD") 66} 67 68func assertResponseInOperation(t *testing.T, op *spec.Operation, isEmpty bool) (result bool) { 69 result = true 70 if op == nil { 71 t.Fatalf("expected a fixture with all REST verbs set") 72 } 73 if op.Responses != nil { 74 if op.Responses.Default != nil { 75 assert.Truef(t, assertResponse(t, "default", op.Responses.Default, isEmpty), 76 "unexpected description in response %s for operation", "default") 77 } 78 for code, resp := range op.Responses.StatusCodeResponses { 79 pin := resp 80 assert.Truef(t, assertResponse(t, strconv.Itoa(code), &pin, isEmpty), 81 "unexpected description in response %d for operation", code) 82 } 83 } 84 return 85} 86func assertResponse(t *testing.T, path string, resp *spec.Response, isEmpty bool) bool { 87 var expected string 88 if isEmpty { 89 expected = "(empty)" 90 } else { 91 expected = "my description" 92 } 93 if resp.Ref.String() != "" { 94 expected = "" 95 } 96 if !assert.Equalf(t, expected, resp.Description, "unexpected description for resp. %s", path) { 97 return false 98 } 99 return true 100} 101