info_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. package spec
  15. import (
  16. "encoding/json"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. const infoJSON = `{
  21. "description": "A sample API that uses a petstore as an example to demonstrate features in ` +
  22. `the swagger-2.0 specification",
  23. "title": "Swagger Sample API",
  24. "termsOfService": "http://helloreverb.com/terms/",
  25. "contact": {
  26. "name": "wordnik api team",
  27. "url": "http://developer.wordnik.com"
  28. },
  29. "license": {
  30. "name": "Creative Commons 4.0 International",
  31. "url": "http://creativecommons.org/licenses/by/4.0/"
  32. },
  33. "version": "1.0.9-abcd",
  34. "x-framework": "go-swagger"
  35. }`
  36. var info = Info{
  37. InfoProps: InfoProps{
  38. Version: "1.0.9-abcd",
  39. Title: "Swagger Sample API",
  40. Description: "A sample API that uses a petstore as an example to demonstrate features in " +
  41. "the swagger-2.0 specification",
  42. TermsOfService: "http://helloreverb.com/terms/",
  43. Contact: &ContactInfo{Name: "wordnik api team", URL: "http://developer.wordnik.com"},
  44. License: &License{
  45. Name: "Creative Commons 4.0 International",
  46. URL: "http://creativecommons.org/licenses/by/4.0/",
  47. },
  48. },
  49. VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}},
  50. }
  51. func TestIntegrationInfo_Serialize(t *testing.T) {
  52. b, err := json.MarshalIndent(info, "", "\t")
  53. if assert.NoError(t, err) {
  54. assert.Equal(t, infoJSON, string(b))
  55. }
  56. }
  57. func TestIntegrationInfo_Deserialize(t *testing.T) {
  58. actual := Info{}
  59. err := json.Unmarshal([]byte(infoJSON), &actual)
  60. if assert.NoError(t, err) {
  61. assert.EqualValues(t, info, actual)
  62. }
  63. }
  64. func TestInfoGobEncoding(t *testing.T) {
  65. var src, dst Info
  66. if assert.NoError(t, json.Unmarshal([]byte(infoJSON), &src)) {
  67. assert.EqualValues(t, src, info)
  68. } else {
  69. t.FailNow()
  70. }
  71. doTestAnyGobEncoding(t, &src, &dst)
  72. }