github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/apidef/importer/importer_test.go (about) 1 package importer 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestToAPIDefinition_Swagger(t *testing.T) { 9 imp, err := GetImporterForSource(SwaggerSource) 10 if err != nil { 11 t.Fatal(err) 12 } 13 14 buff := bytes.NewBufferString(petstoreJSON) 15 16 err = imp.LoadFrom(buff) 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 def, err := imp.ToAPIDefinition("testOrg", "http://test.com", false) 22 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 if def.VersionData.NotVersioned { 28 t.Fatal("Swagger import must always be versioned") 29 } 30 31 if len(def.VersionData.Versions) > 1 { 32 t.Fatal("THere should only be one version") 33 } 34 35 v, ok := def.VersionData.Versions["1.0.0"] 36 if !ok { 37 t.Fatal("Version could not be found") 38 } 39 40 if len(v.ExtendedPaths.TrackEndpoints) != 3 { 41 t.Fatalf("Expected 3 endpoints, found %v\n", len(v.ExtendedPaths.TrackEndpoints)) 42 } 43 44 } 45 46 var petstoreJSON string = `{ 47 "swagger": "2.0", 48 "info": { 49 "version": "1.0.0", 50 "title": "Swagger Petstore", 51 "license": { 52 "name": "MIT" 53 } 54 }, 55 "host": "petstore.swagger.io", 56 "basePath": "/v1", 57 "schemes": [ 58 "http" 59 ], 60 "consumes": [ 61 "application/json" 62 ], 63 "produces": [ 64 "application/json" 65 ], 66 "paths": { 67 "/pets": { 68 "get": { 69 "summary": "List all pets", 70 "operationId": "listPets", 71 "tags": [ 72 "pets" 73 ], 74 "parameters": [ 75 { 76 "name": "limit", 77 "in": "query", 78 "description": "How many items to return at one time (max 100)", 79 "required": false, 80 "type": "integer", 81 "format": "int32" 82 } 83 ], 84 "responses": { 85 "200": { 86 "description": "An paged array of pets", 87 "headers": { 88 "x-next": { 89 "type": "string", 90 "description": "A link to the next page of responses" 91 } 92 }, 93 "schema": { 94 "$ref": "#/definitions/Pets" 95 } 96 }, 97 "default": { 98 "description": "unexpected error", 99 "schema": { 100 "$ref": "#/definitions/Error" 101 } 102 } 103 } 104 }, 105 "post": { 106 "summary": "Create a pet", 107 "operationId": "createPets", 108 "tags": [ 109 "pets" 110 ], 111 "responses": { 112 "201": { 113 "description": "Null response" 114 }, 115 "default": { 116 "description": "unexpected error", 117 "schema": { 118 "$ref": "#/definitions/Error" 119 } 120 } 121 } 122 } 123 }, 124 "/pets/{petId}": { 125 "get": { 126 "summary": "Info for a specific pet", 127 "operationId": "showPetById", 128 "tags": [ 129 "pets" 130 ], 131 "parameters": [ 132 { 133 "name": "petId", 134 "in": "path", 135 "required": true, 136 "description": "The id of the pet to retrieve", 137 "type": "string" 138 } 139 ], 140 "responses": { 141 "200": { 142 "description": "Expected response to a valid request", 143 "schema": { 144 "$ref": "#/definitions/Pets" 145 } 146 }, 147 "default": { 148 "description": "unexpected error", 149 "schema": { 150 "$ref": "#/definitions/Error" 151 } 152 } 153 } 154 } 155 } 156 }, 157 "definitions": { 158 "Pet": { 159 "required": [ 160 "id", 161 "name" 162 ], 163 "properties": { 164 "id": { 165 "type": "integer", 166 "format": "int64" 167 }, 168 "name": { 169 "type": "string" 170 }, 171 "tag": { 172 "type": "string" 173 } 174 } 175 }, 176 "Pets": { 177 "type": "array", 178 "items": { 179 "$ref": "#/definitions/Pet" 180 } 181 }, 182 "Error": { 183 "required": [ 184 "code", 185 "message" 186 ], 187 "properties": { 188 "code": { 189 "type": "integer", 190 "format": "int32" 191 }, 192 "message": { 193 "type": "string" 194 } 195 } 196 } 197 } 198 }`