github.com/Axway/agent-sdk@v1.1.101/pkg/util/oas/oas_test.go (about)

     1  package oas
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  var petstore3Json = `{
    10  	"openapi": "3.0.1",
    11  	"info": {
    12  		"title": "petstore3"
    13  	},
    14  	"paths": {},
    15  	"servers": [
    16  		{
    17  			"url": "https://google.com"
    18  		}
    19  	]
    20  }`
    21  
    22  var petstore2Json = `{
    23  	"basePath": "/v2",
    24  	"host": "host.com",
    25  	"schemes": [
    26  		"http"
    27  	],
    28  	"swagger": "2.0",
    29  	"info": {
    30  		"title": "petstore2"
    31  	},
    32  	"paths": {}
    33  }`
    34  
    35  func TestParseOAS3(t *testing.T) {
    36  	tests := []struct {
    37  		name     string
    38  		hasError bool
    39  		spec     string
    40  	}{
    41  		{
    42  			name:     "Should parse the OAS3 spec as json",
    43  			hasError: false,
    44  			spec:     petstore3Json,
    45  		},
    46  		{
    47  			name:     "Should fail to parse the spec when the 'openapi' key is incorrect",
    48  			hasError: true,
    49  			spec: `{
    50  				"openapi": "2.1.1",
    51  				"info": {
    52  					"title": "petstore3"
    53  				},
    54  				"paths": {},
    55  				"servers": [
    56  					{
    57  						"url": "https://google.com"
    58  					}
    59  				]
    60  			}`,
    61  		},
    62  		{
    63  			name:     "Should fail to parse the spec when the 'paths' key is missing",
    64  			hasError: true,
    65  			spec: `{
    66  				"openapi": "3.0.1",
    67  				"info": {
    68  					"title": "petstore3"
    69  				},
    70  				"servers": [
    71  					{
    72  						"url": "https://google.com"
    73  					}
    74  				]
    75  			}`,
    76  		},
    77  		{
    78  			name:     "Should fail to parse the spec when the 'info' key is missing",
    79  			hasError: true,
    80  			spec: `{
    81  				"openapi": "3.0.1",
    82  				"paths": {},
    83  				"servers": [
    84  					{
    85  						"url": "https://google.com"
    86  					}
    87  				]
    88  			}`,
    89  		},
    90  		{
    91  			name:     "Should fail to parse the spec when the 'info.title' key is missing",
    92  			hasError: true,
    93  			spec: `{
    94  				"openapi": "3.0.1",
    95  				"info": {
    96  				},
    97  				"paths": {},
    98  				"servers": [
    99  					{
   100  						"url": "https://google.com"
   101  					}
   102  				]
   103  			}`,
   104  		},
   105  	}
   106  	for _, tc := range tests {
   107  		t.Run(tc.name, func(t *testing.T) {
   108  			_, err := ParseOAS3([]byte(tc.spec))
   109  			if tc.hasError == false {
   110  				assert.Nil(t, err)
   111  			} else {
   112  				assert.NotNil(t, err)
   113  			}
   114  		})
   115  	}
   116  }
   117  
   118  func TestSetServers(t *testing.T) {
   119  	tests := []struct {
   120  		name    string
   121  		servers []string
   122  	}{
   123  		{
   124  			name:    "Should update the servers field with the provided hosts",
   125  			servers: []string{"http://abc.com", "https://123.com"},
   126  		},
   127  	}
   128  	for _, tc := range tests {
   129  		t.Run(tc.name, func(t *testing.T) {
   130  			obj, err := ParseOAS3([]byte(petstore3Json))
   131  			assert.Nil(t, err)
   132  			SetOAS3Servers(tc.servers, obj)
   133  			assert.Equal(t, len(tc.servers), len(obj.Servers))
   134  			assert.Equal(t, tc.servers[0], obj.Servers[0].URL)
   135  			assert.Equal(t, tc.servers[1], obj.Servers[1].URL)
   136  		})
   137  	}
   138  }
   139  
   140  func TestParseOAS2(t *testing.T) {
   141  	tests := []struct {
   142  		name     string
   143  		hasError bool
   144  		spec     string
   145  	}{
   146  		{
   147  			name:     "Should parse the OAS2 spec as json",
   148  			hasError: false,
   149  			spec:     petstore2Json,
   150  		},
   151  		{
   152  			name:     "Should fail to parse the spec when the 'swagger' key is incorrect",
   153  			hasError: true,
   154  			spec: `{
   155  				"swagger": "1.1",
   156  				"info": {
   157  						"title": "petstore2"
   158  				},
   159  				"paths": {}
   160  			}`,
   161  		},
   162  		{
   163  			name:     "Should fail to parse the spec when the 'paths' key is missing",
   164  			hasError: true,
   165  			spec: `{
   166  				"swagger": "2.0",
   167  				"info": {
   168  						"title": "petstore2"
   169  				},
   170  			}`,
   171  		},
   172  		{
   173  			name:     "Should fail to parse the spec when the 'title' key is missing",
   174  			hasError: true,
   175  			spec: `{
   176  				"swagger": "2.0"
   177  			}`,
   178  		},
   179  	}
   180  	for _, tc := range tests {
   181  		t.Run(tc.name, func(t *testing.T) {
   182  			_, err := ParseOAS2([]byte(tc.spec))
   183  			if tc.hasError == false {
   184  				assert.Nil(t, err)
   185  			} else {
   186  				assert.NotNil(t, err)
   187  			}
   188  		})
   189  	}
   190  }
   191  
   192  func TestSetOAS2HostDetails(t *testing.T) {
   193  	tests := []struct {
   194  		name        string
   195  		endpointURL string
   196  		host        string
   197  		schemes     []string
   198  		basePath    string
   199  	}{
   200  		{
   201  			name:        "Should update the spec with the provided host",
   202  			endpointURL: "https://newhost.com/v2",
   203  			host:        "newhost.com",
   204  			basePath:    "/v2",
   205  			schemes:     []string{"https"},
   206  		},
   207  		{
   208  			name:        "Should update the spec with the provided host, and set the basePath to '/'",
   209  			endpointURL: "http://newhost.com",
   210  			host:        "newhost.com",
   211  			basePath:    "/",
   212  			schemes:     []string{"http"},
   213  		},
   214  	}
   215  	for _, tc := range tests {
   216  		t.Run(tc.name, func(t *testing.T) {
   217  			obj, err := ParseOAS2([]byte(petstore2Json))
   218  			assert.Nil(t, err)
   219  			err = SetOAS2HostDetails(obj, tc.endpointURL)
   220  			assert.Equal(t, obj.Host, tc.host)
   221  			assert.Equal(t, obj.BasePath, tc.basePath)
   222  			assert.Equal(t, obj.Schemes, tc.schemes)
   223  		})
   224  	}
   225  }