github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/scan/operations_test.go (about) 1 //go:build !go1.11 2 // +build !go1.11 3 4 // Copyright 2015 go-swagger maintainers 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package scan 19 20 import ( 21 goparser "go/parser" 22 "log" 23 "testing" 24 25 "github.com/go-openapi/spec" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestOperationsExpression(t *testing.T) { 30 assert.Regexp(t, rxOperation, "swagger:operation DELETE /orders/{id} deleteOrder") 31 assert.Regexp(t, rxOperation, "swagger:operation GET /v1.2/something deleteOrder") 32 } 33 34 func TestOperationsParser(t *testing.T) { 35 docFile := "../fixtures/goparsing/classification/operations_annotation/operations.go" 36 fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments) 37 if err != nil { 38 log.Fatal(err) 39 } 40 41 op := newOperationsParser(classificationProg) 42 var ops spec.Paths 43 err = op.Parse(fileTree, &ops, nil, nil) 44 assert.NoError(t, err) 45 46 assert.Len(t, ops.Paths, 3) 47 48 po, ok := ops.Paths["/pets"] 49 assert.True(t, ok) 50 assert.NotNil(t, po.Get) 51 assertAnnotationOperation(t, 52 po.Get, 53 "getPet", 54 "", 55 "List all pets", 56 []string{"pets"}, 57 ) 58 if po.Get != nil { 59 rsp, k := po.Get.Responses.StatusCodeResponses[200] 60 if assert.True(t, k) { 61 assert.Equal(t, "An paged array of pets", rsp.Description) 62 } 63 if assert.NotNil(t, po.Get.Responses.Default) { 64 assert.Equal(t, "unexpected error", po.Get.Responses.Default.Description) 65 } 66 } 67 68 po, ok = ops.Paths["/pets/{id}"] 69 assert.True(t, ok) 70 assert.NotNil(t, po.Put) 71 assertAnnotationOperation(t, 72 po.Put, 73 "updatePet", 74 "Updates the details for a pet.", 75 "Some long explanation,\nspanning over multipele lines,\nAKA the description.", 76 []string{"pets"}, 77 ) 78 if po.Put != nil { 79 rsp, k := po.Put.Responses.StatusCodeResponses[400] 80 if assert.True(t, k) { 81 assert.Equal(t, "Invalid ID supplied", rsp.Description) 82 } 83 rsp, k = po.Put.Responses.StatusCodeResponses[404] 84 if assert.True(t, k) { 85 assert.Equal(t, "Pet not found", rsp.Description) 86 } 87 rsp, k = po.Put.Responses.StatusCodeResponses[405] 88 if assert.True(t, k) { 89 assert.Equal(t, "Validation exception", rsp.Description) 90 } 91 } 92 93 po, ok = ops.Paths["/v1/events"] 94 assert.True(t, ok) 95 assert.NotNil(t, po.Get) 96 assertAnnotationOperation(t, 97 po.Get, 98 "getEvents", 99 "Events", 100 "Mitigation Events", 101 []string{"Events"}, 102 ) 103 if po.Get != nil { 104 rsp, k := po.Get.Responses.StatusCodeResponses[200] 105 if assert.True(t, k) { 106 assert.Equal(t, "#/definitions/ListResponse", rsp.Schema.Ref.String()) 107 assert.Equal(t, "200", rsp.Description) 108 } 109 rsp, k = po.Get.Responses.StatusCodeResponses[400] 110 if assert.True(t, k) { 111 assert.Equal(t, "#/definitions/ErrorResponse", rsp.Schema.Ref.String()) 112 assert.Equal(t, "400", rsp.Description) 113 } 114 } 115 } 116 117 func assertAnnotationOperation(t *testing.T, op *spec.Operation, id, summary, description string, tags []string) { 118 assert.NotNil(t, op) 119 assert.Equal(t, summary, op.Summary) 120 assert.Equal(t, description, op.Description) 121 assert.Equal(t, id, op.ID) 122 assert.EqualValues(t, tags, op.Tags) 123 assert.Contains(t, op.Consumes, "application/json") 124 assert.Contains(t, op.Consumes, "application/xml") 125 assert.Contains(t, op.Produces, "application/json") 126 assert.Contains(t, op.Produces, "application/xml") 127 assert.Len(t, op.Security, 1) 128 if len(op.Security) > 0 { 129 akv, ok := op.Security[0]["petstore_auth"] 130 assert.True(t, ok) 131 // akv must be defined & not empty 132 assert.NotNil(t, akv) 133 assert.NotEmpty(t, akv) 134 } 135 }