$54 GRAYBYTE WORDPRESS FILE MANAGER $81

SERVER : in-mum-web1330.main-hosting.eu #1 SMP Mon Feb 10 22:45:17 UTC 2025
SERVER IP : 2.57.91.242 | ADMIN IP 216.73.216.215
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/go/pkg/mod/github.com/go-openapi/spec@v0.20.8/

HOME
Current File : /opt/go/pkg/mod/github.com/go-openapi/spec@v0.20.8//swagger_test.go
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package spec

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var spec = Swagger{
	SwaggerProps: SwaggerProps{
		ID:          "http://localhost:3849/api-docs",
		Swagger:     "2.0",
		Consumes:    []string{"application/json", "application/x-yaml"},
		Produces:    []string{"application/json"},
		Schemes:     []string{"http", "https"},
		Info:        &info,
		Host:        "some.api.out.there",
		BasePath:    "/",
		Paths:       &paths,
		Definitions: map[string]Schema{"Category": {SchemaProps: SchemaProps{Type: []string{"string"}}}},
		Parameters: map[string]Parameter{
			"categoryParam": {ParamProps: ParamProps{Name: "category", In: "query"}, SimpleSchema: SimpleSchema{Type: "string"}},
		},
		Responses: map[string]Response{
			"EmptyAnswer": {
				ResponseProps: ResponseProps{
					Description: "no data to return for this operation",
				},
			},
		},
		SecurityDefinitions: map[string]*SecurityScheme{
			"internalApiKey": APIKeyAuth("api_key", "header"),
		},
		Security: []map[string][]string{
			{"internalApiKey": {}},
		},
		Tags:         []Tag{NewTag("pets", "", nil)},
		ExternalDocs: &ExternalDocumentation{Description: "the name", URL: "the url"},
	},
	VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{
		"x-some-extension": "vendor",
		"x-schemes":        []interface{}{"unix", "amqp"},
	}},
}

const specJSON = `{
	"id": "http://localhost:3849/api-docs",
	"consumes": ["application/json", "application/x-yaml"],
	"produces": ["application/json"],
	"schemes": ["http", "https"],
	"swagger": "2.0",
	"info": {
		"contact": {
			"name": "wordnik api team",
			"url": "http://developer.wordnik.com"
		},
		"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0` +
	` specification",
		"license": {
			"name": "Creative Commons 4.0 International",
			"url": "http://creativecommons.org/licenses/by/4.0/"
		},
		"termsOfService": "http://helloreverb.com/terms/",
		"title": "Swagger Sample API",
		"version": "1.0.9-abcd",
		"x-framework": "go-swagger"
	},
	"host": "some.api.out.there",
	"basePath": "/",
	"paths": {"x-framework":"go-swagger","/":{"$ref":"cats"}},
	"definitions": { "Category": { "type": "string"} },
	"parameters": {
		"categoryParam": {
			"name": "category",
			"in": "query",
			"type": "string"
		}
	},
	"responses": { "EmptyAnswer": { "description": "no data to return for this operation" } },
	"securityDefinitions": {
		"internalApiKey": {
			"type": "apiKey",
			"in": "header",
			"name": "api_key"
		}
	},
	"security": [{"internalApiKey":[]}],
	"tags": [{"name":"pets"}],
	"externalDocs": {"description":"the name","url":"the url"},
	"x-some-extension": "vendor",
	"x-schemes": ["unix","amqp"]
}`

//
// func verifySpecSerialize(specJSON []byte, spec Swagger) {
// 	expected := map[string]interface{}{}
// 	json.Unmarshal(specJSON, &expected)
// 	b, err := json.MarshalIndent(spec, "", "  ")
// 	So(err, ShouldBeNil)
// 	var actual map[string]interface{}
// 	err = json.Unmarshal(b, &actual)
// 	So(err, ShouldBeNil)
// 	compareSpecMaps(actual, expected)
// }

/*
// assertEquivalent is currently unused
func assertEquivalent(t testing.TB, actual, expected interface{}) bool {
	if actual == nil || expected == nil || reflect.DeepEqual(actual, expected) {
		return true
	}

	actualType := reflect.TypeOf(actual)
	expectedType := reflect.TypeOf(expected)
	if reflect.TypeOf(actual).ConvertibleTo(expectedType) {
		expectedValue := reflect.ValueOf(expected)
		if swag.IsZero(expectedValue) && swag.IsZero(reflect.ValueOf(actual)) {
			return true
		}

		// Attempt comparison after type conversion
		if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) {
			return true
		}
	}

	// Last ditch effort
	if fmt.Sprintf("%#v", expected) == fmt.Sprintf("%#v", actual) {
		return true
	}
	errFmt := "Expected: '%T(%#v)'\nActual:   '%T(%#v)'\n(Should be equivalent)!"
	return assert.Fail(t, errFmt, expected, expected, actual, actual)
}

// ShouldBeEquivalentTo is currently unused
func ShouldBeEquivalentTo(actual interface{}, expecteds ...interface{}) string {
	expected := expecteds[0]
	if actual == nil || expected == nil {
		return ""
	}

	if reflect.DeepEqual(expected, actual) {
		return ""
	}

	actualType := reflect.TypeOf(actual)
	expectedType := reflect.TypeOf(expected)
	if reflect.TypeOf(actual).ConvertibleTo(expectedType) {
		expectedValue := reflect.ValueOf(expected)
		if swag.IsZero(expectedValue) && swag.IsZero(reflect.ValueOf(actual)) {
			return ""
		}

		// Attempt comparison after type conversion
		if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) {
			return ""
		}
	}

	// Last ditch effort
	if fmt.Sprintf("%#v", expected) == fmt.Sprintf("%#v", actual) {
		return ""
	}
	errFmt := "Expected: '%T(%#v)'\nActual:   '%T(%#v)'\n(Should be equivalent)!"
	return fmt.Sprintf(errFmt, expected, expected, actual, actual)

}

// assertSpecMaps is currently unused
func assertSpecMaps(t testing.TB, actual, expected map[string]interface{}) bool {
	res := true
	if id, ok := expected["id"]; ok {
		res = assert.Equal(t, id, actual["id"])
	}
	res = res && assert.Equal(t, expected["consumes"], actual["consumes"])
	res = res && assert.Equal(t, expected["produces"], actual["produces"])
	res = res && assert.Equal(t, expected["schemes"], actual["schemes"])
	res = res && assert.Equal(t, expected["swagger"], actual["swagger"])
	res = res && assert.Equal(t, expected["info"], actual["info"])
	res = res && assert.Equal(t, expected["host"], actual["host"])
	res = res && assert.Equal(t, expected["basePath"], actual["basePath"])
	res = res && assert.Equal(t, expected["paths"], actual["paths"])
	res = res && assert.Equal(t, expected["definitions"], actual["definitions"])
	res = res && assert.Equal(t, expected["responses"], actual["responses"])
	res = res && assert.Equal(t, expected["securityDefinitions"], actual["securityDefinitions"])
	res = res && assert.Equal(t, expected["tags"], actual["tags"])
	res = res && assert.Equal(t, expected["externalDocs"], actual["externalDocs"])
	res = res && assert.Equal(t, expected["x-some-extension"], actual["x-some-extension"])
	res = res && assert.Equal(t, expected["x-schemes"], actual["x-schemes"])

	return res
}
*/
func assertSpecs(t testing.TB, actual, expected Swagger) bool {
	expected.Swagger = "2.0"
	return assert.Equal(t, actual, expected)
}

/*
// assertSpecJSON is currently unused
func assertSpecJSON(t testing.TB, specJSON []byte) bool {
	var expected map[string]interface{}
	if !assert.NoError(t, json.Unmarshal(specJSON, &expected)) {
		return false
	}

	obj := Swagger{}
	if !assert.NoError(t, json.Unmarshal(specJSON, &obj)) {
		return false
	}

	cb, err := json.MarshalIndent(obj, "", "  ")
	if assert.NoError(t, err) {
		return false
	}
	var actual map[string]interface{}
	if !assert.NoError(t, json.Unmarshal(cb, &actual)) {
		return false
	}
	return assertSpecMaps(t, actual, expected)
}
*/

func TestSwaggerSpec_Serialize(t *testing.T) {
	expected := make(map[string]interface{})
	_ = json.Unmarshal([]byte(specJSON), &expected)
	b, err := json.MarshalIndent(spec, "", "  ")
	if assert.NoError(t, err) {
		var actual map[string]interface{}
		err := json.Unmarshal(b, &actual)
		if assert.NoError(t, err) {
			assert.EqualValues(t, actual, expected)
		}
	}
}

func TestSwaggerSpec_Deserialize(t *testing.T) {
	var actual Swagger
	err := json.Unmarshal([]byte(specJSON), &actual)
	if assert.NoError(t, err) {
		assert.EqualValues(t, actual, spec)
	}
}

func TestVendorExtensionStringSlice(t *testing.T) {
	var actual Swagger
	err := json.Unmarshal([]byte(specJSON), &actual)
	if assert.NoError(t, err) {
		schemes, ok := actual.Extensions.GetStringSlice("x-schemes")
		if assert.True(t, ok) {
			assert.EqualValues(t, []string{"unix", "amqp"}, schemes)
		}
		notSlice, ok := actual.Extensions.GetStringSlice("x-some-extension")
		assert.Nil(t, notSlice)
		assert.False(t, ok)

		actual.AddExtension("x-another-ext", 100)
		notString, ok := actual.Extensions.GetStringSlice("x-another-ext")
		assert.Nil(t, notString)
		assert.False(t, ok)

		actual.AddExtension("x-another-slice-ext", []interface{}{100, 100})
		notStringSlice, ok := actual.Extensions.GetStringSlice("x-another-slice-ext")
		assert.Nil(t, notStringSlice)
		assert.False(t, ok)

		_, ok = actual.Extensions.GetStringSlice("x-notfound-ext")
		assert.False(t, ok)
	}
}

func TestOptionalSwaggerProps_Serialize(t *testing.T) {
	minimalJSONSpec := []byte(`{
	"swagger": "2.0",
	"info": {
		"version": "0.0.0",
		"title": "Simple API"
	},
	"paths": {
		"/": {
			"get": {
				"responses": {
					"200": {
						"description": "OK"
					}
				}
			}
		}
	}
}`)

	var minimalSpec Swagger
	err := json.Unmarshal(minimalJSONSpec, &minimalSpec)
	if assert.NoError(t, err) {
		bytes, err := json.Marshal(&minimalSpec)
		if assert.NoError(t, err) {
			var ms map[string]interface{}
			if err := json.Unmarshal(bytes, &ms); assert.NoError(t, err) {
				assert.NotContains(t, ms, "consumes")
				assert.NotContains(t, ms, "produces")
				assert.NotContains(t, ms, "schemes")
				assert.NotContains(t, ms, "host")
				assert.NotContains(t, ms, "basePath")
				assert.NotContains(t, ms, "definitions")
				assert.NotContains(t, ms, "parameters")
				assert.NotContains(t, ms, "responses")
				assert.NotContains(t, ms, "securityDefinitions")
				assert.NotContains(t, ms, "security")
				assert.NotContains(t, ms, "tags")
				assert.NotContains(t, ms, "externalDocs")
			}
		}
	}
}

var minimalJSONSpec = []byte(`{
		"swagger": "2.0",
		"info": {
			"version": "0.0.0",
			"title": "Simple API"
		},
		"securityDefinitions": {
			"basic": {
				"type": "basic"
			},
			"apiKey": {
				"type": "apiKey",
				"in": "header",
				"name": "X-API-KEY"
			},
			"queryKey": {
				"type": "apiKey",
				"in": "query",
				"name": "api_key"
			}
		},
		"paths": {
			"/": {
				"get": {
					"security": [
						{
							"apiKey": [],
							"basic": []
						},
						{},
						{
							"queryKey": [],
							"basic": []
						}
					],
					"responses": {
						"200": {
							"description": "OK"
						}
					}
				}
			}
		}
	}`)

func TestSecurityRequirements(t *testing.T) {
	var minimalSpec Swagger
	err := json.Unmarshal(minimalJSONSpec, &minimalSpec)
	if assert.NoError(t, err) {
		sec := minimalSpec.Paths.Paths["/"].Get.Security
		require.Len(t, sec, 3)
		assert.Contains(t, sec[0], "basic")
		assert.Contains(t, sec[0], "apiKey")
		assert.NotNil(t, sec[1])
		assert.Empty(t, sec[1])
		assert.Contains(t, sec[2], "queryKey")
	}
}

func TestSwaggerGobEncoding(t *testing.T) {
	doTestSwaggerGobEncoding(t, specJSON)

	doTestSwaggerGobEncoding(t, string(minimalJSONSpec))
}

func doTestSwaggerGobEncoding(t *testing.T, fixture string) {
	var src, dst Swagger

	if !assert.NoError(t, json.Unmarshal([]byte(fixture), &src)) {
		t.FailNow()
	}

	doTestAnyGobEncoding(t, &src, &dst)
}

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
25 Jan 2024 4.43 PM
root / root
0755
.github
--
25 Jan 2024 4.43 PM
root / root
0555
fixtures
--
25 Jan 2024 4.43 PM
root / root
0555
schemas
--
25 Jan 2024 4.43 PM
root / root
0555
.editorconfig
0.526 KB
25 Jan 2024 4.43 PM
root / root
0444
.gitignore
0.024 KB
25 Jan 2024 4.43 PM
root / root
0444
.golangci.yml
0.621 KB
25 Jan 2024 4.43 PM
root / root
0444
CODE_OF_CONDUCT.md
3.157 KB
25 Jan 2024 4.43 PM
root / root
0444
LICENSE
11.092 KB
25 Jan 2024 4.43 PM
root / root
0444
README.md
2.114 KB
25 Jan 2024 4.43 PM
root / root
0444
appveyor.yml
1.261 KB
25 Jan 2024 4.43 PM
root / root
0444
auth_test.go
4.577 KB
25 Jan 2024 4.43 PM
root / root
0444
bindata.go
24.402 KB
25 Jan 2024 4.43 PM
root / root
0444
cache.go
2.507 KB
25 Jan 2024 4.43 PM
root / root
0444
cache_test.go
0.66 KB
25 Jan 2024 4.43 PM
root / root
0444
circular_test.go
9.746 KB
25 Jan 2024 4.43 PM
root / root
0444
contact_info.go
1.574 KB
25 Jan 2024 4.43 PM
root / root
0444
contact_info_test.go
1.422 KB
25 Jan 2024 4.43 PM
root / root
0444
debug.go
1.272 KB
25 Jan 2024 4.43 PM
root / root
0444
debug_test.go
1.293 KB
25 Jan 2024 4.43 PM
root / root
0444
errors.go
0.881 KB
25 Jan 2024 4.43 PM
root / root
0444
expander.go
16.719 KB
25 Jan 2024 4.43 PM
root / root
0444
expander_test.go
40.121 KB
25 Jan 2024 4.43 PM
root / root
0444
external_docs.go
0.896 KB
25 Jan 2024 4.43 PM
root / root
0444
external_docs_test.go
1.045 KB
25 Jan 2024 4.43 PM
root / root
0444
go.mod
0.236 KB
25 Jan 2024 4.43 PM
root / root
0444
go.sum
3.695 KB
25 Jan 2024 4.43 PM
root / root
0444
header.go
4.943 KB
25 Jan 2024 4.43 PM
root / root
0444
header_test.go
4.348 KB
25 Jan 2024 4.43 PM
root / root
0444
helpers_spec_test.go
3.971 KB
25 Jan 2024 4.43 PM
root / root
0444
helpers_test.go
4.854 KB
25 Jan 2024 4.43 PM
root / root
0444
info.go
4.256 KB
25 Jan 2024 4.43 PM
root / root
0444
info_test.go
2.407 KB
25 Jan 2024 4.43 PM
root / root
0444
items.go
5.755 KB
25 Jan 2024 4.43 PM
root / root
0444
items_test.go
4.494 KB
25 Jan 2024 4.43 PM
root / root
0444
license.go
1.48 KB
25 Jan 2024 4.43 PM
root / root
0444
license_test.go
1.322 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer.go
6.241 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer_nonwindows.go
1.191 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer_test.go
16.808 KB
25 Jan 2024 4.43 PM
root / root
0444
normalizer_windows.go
5.146 KB
25 Jan 2024 4.43 PM
root / root
0444
operation.go
10.751 KB
25 Jan 2024 4.43 PM
root / root
0444
operation_test.go
9.549 KB
25 Jan 2024 4.43 PM
root / root
0444
parameter.go
10.344 KB
25 Jan 2024 4.43 PM
root / root
0444
parameters_test.go
4.756 KB
25 Jan 2024 4.43 PM
root / root
0444
path_item.go
2.596 KB
25 Jan 2024 4.43 PM
root / root
0444
path_item_test.go
2.42 KB
25 Jan 2024 4.43 PM
root / root
0444
paths.go
2.571 KB
25 Jan 2024 4.43 PM
root / root
0444
paths_test.go
1.167 KB
25 Jan 2024 4.43 PM
root / root
0444
properties.go
2.403 KB
25 Jan 2024 4.43 PM
root / root
0444
properties_test.go
2.054 KB
25 Jan 2024 4.43 PM
root / root
0444
ref.go
4.167 KB
25 Jan 2024 4.43 PM
root / root
0444
ref_test.go
1.19 KB
25 Jan 2024 4.43 PM
root / root
0444
resolver.go
3.752 KB
25 Jan 2024 4.43 PM
root / root
0444
resolver_test.go
12.791 KB
25 Jan 2024 4.43 PM
root / root
0444
response.go
4.166 KB
25 Jan 2024 4.43 PM
root / root
0444
response_test.go
2.817 KB
25 Jan 2024 4.43 PM
root / root
0444
responses.go
3.619 KB
25 Jan 2024 4.43 PM
root / root
0444
schema.go
18.14 KB
25 Jan 2024 4.43 PM
root / root
0444
schema_loader.go
8.993 KB
25 Jan 2024 4.43 PM
root / root
0444
schema_test.go
6.643 KB
25 Jan 2024 4.43 PM
root / root
0444
security_scheme.go
5.713 KB
25 Jan 2024 4.43 PM
root / root
0444
spec.go
2.18 KB
25 Jan 2024 4.43 PM
root / root
0444
spec_test.go
12.704 KB
25 Jan 2024 4.43 PM
root / root
0444
structs_test.go
3.88 KB
25 Jan 2024 4.43 PM
root / root
0444
swagger.go
11.854 KB
25 Jan 2024 4.43 PM
root / root
0444
swagger_test.go
11.273 KB
25 Jan 2024 4.43 PM
root / root
0444
tag.go
2.235 KB
25 Jan 2024 4.43 PM
root / root
0444
url_go18.go
0.093 KB
25 Jan 2024 4.43 PM
root / root
0444
url_go19.go
0.189 KB
25 Jan 2024 4.43 PM
root / root
0444
validations.go
7.121 KB
25 Jan 2024 4.43 PM
root / root
0444
validations_test.go
2.969 KB
25 Jan 2024 4.43 PM
root / root
0444
xml_object.go
1.928 KB
25 Jan 2024 4.43 PM
root / root
0444
xml_object_test.go
1.938 KB
25 Jan 2024 4.43 PM
root / root
0444

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF