|
| 1 | +import expect, {createSpy, spyOn} from 'expect' |
| 2 | +import xmock from 'xmock' |
| 3 | +import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '../src/execute' |
| 4 | + |
| 5 | +// Supported shape... { spec, operationId, parameters, securities, fetch } |
| 6 | +// One can use operationId or pathItem + method |
| 7 | + |
| 8 | +describe('baseUrl', () => { |
| 9 | + |
| 10 | + describe('with contextUrl', () => { |
| 11 | + |
| 12 | + }) |
| 13 | + |
| 14 | + it('should calculate a valid baseUrl given host, basePath, context, schemes', () => { |
| 15 | + const res = baseUrl({ |
| 16 | + spec: { |
| 17 | + schemes: ['https'], |
| 18 | + host: 'foo.com:8080', |
| 19 | + basePath: '/bar' |
| 20 | + }, |
| 21 | + contextUrl: 'http://example.com:9090/hello/swagger.json' |
| 22 | + }) |
| 23 | + |
| 24 | + expect(res).toEqual('https://foo.com:8080/bar') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should calculate a valid baseUrl given host, basePath, context', () => { |
| 28 | + const res = baseUrl({ |
| 29 | + spec: { |
| 30 | + host: 'foo.com:8080', |
| 31 | + basePath: '/bar' |
| 32 | + }, |
| 33 | + contextUrl: 'http://example.com:9090/hello/swagger.json' |
| 34 | + }) |
| 35 | + |
| 36 | + expect(res).toEqual('http://foo.com:8080/bar') |
| 37 | + }) |
| 38 | + |
| 39 | + it('should trim the trailing slash when basePath is "/"', () => { |
| 40 | + const res = baseUrl({ |
| 41 | + spec: { |
| 42 | + host: 'foo.com:8080', |
| 43 | + basePath: '/' |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + expect(res).toEqual('http://foo.com:8080') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should infer the host and port based on the contextUrl', () => { |
| 51 | + const res = baseUrl({ |
| 52 | + spec: { |
| 53 | + basePath: '/bar' |
| 54 | + }, |
| 55 | + contextUrl: "http://example.com:9090/hello/swagger.json" |
| 56 | + }) |
| 57 | + |
| 58 | + expect(res).toEqual("http://example.com:9090/bar") |
| 59 | + }) |
| 60 | + |
| 61 | + it('should infer the entire url based on the contextUrl', () => { |
| 62 | + const res = baseUrl({ |
| 63 | + spec: {}, |
| 64 | + contextUrl: "http://example.com:9090/hello/swagger.json" |
| 65 | + }) |
| 66 | + |
| 67 | + expect(res).toEqual("http://example.com:9090") |
| 68 | + }) |
| 69 | + |
| 70 | + it('should infer the host based on the contextUrl', () => { |
| 71 | + const res = baseUrl({ |
| 72 | + spec: { |
| 73 | + schemes: ['https'], |
| 74 | + basePath: '/bar' |
| 75 | + }, |
| 76 | + contextUrl: "http://example.com:9090/hello/swagger.json" |
| 77 | + }) |
| 78 | + |
| 79 | + expect(res).toEqual("https://example.com:9090/bar") |
| 80 | + }) |
| 81 | + |
| 82 | + it('should default to an empty basePath', () => { |
| 83 | + const res = baseUrl({ |
| 84 | + spec: { |
| 85 | + schemes: ['https'], |
| 86 | + host: 'foo.com:8080' |
| 87 | + }, |
| 88 | + contextUrl: "http://example.com:9090/hello/swagger.json" |
| 89 | + }) |
| 90 | + |
| 91 | + expect(res).toEqual("https://foo.com:8080") |
| 92 | + }) |
| 93 | + |
| 94 | + it('should default to the correct scheme based on the spec', () => { |
| 95 | + const res = baseUrl({ |
| 96 | + spec: { |
| 97 | + schemes: ['https'] |
| 98 | + }, |
| 99 | + contextUrl: "http://example.com:9090/hello/swagger.json" |
| 100 | + }) |
| 101 | + |
| 102 | + expect(res).toEqual("https://example.com:9090") |
| 103 | + }) |
| 104 | + |
| 105 | + it('should default to HTTP scheme based on the contextUrl', () => { |
| 106 | + const res = baseUrl({ |
| 107 | + spec: { |
| 108 | + host: 'foo.com:8080' |
| 109 | + }, |
| 110 | + contextUrl: "http://example.com:9090/hello/swagger.json" |
| 111 | + }) |
| 112 | + |
| 113 | + expect(res).toEqual("http://foo.com:8080") |
| 114 | + }) |
| 115 | + |
| 116 | + it('should default to HTTPS scheme based on the contextUrl', () => { |
| 117 | + const res = baseUrl({ |
| 118 | + spec: { |
| 119 | + host: 'foo.com:8080' |
| 120 | + }, |
| 121 | + contextUrl: "https://example.com:9090/hello/swagger.json" |
| 122 | + }) |
| 123 | + |
| 124 | + expect(res).toEqual("https://foo.com:8080") |
| 125 | + }) |
| 126 | + |
| 127 | + it('should default to HTTPS scheme and host based on the contextUrl', () => { |
| 128 | + const res = baseUrl({ |
| 129 | + spec: { |
| 130 | + title: 'a spec' |
| 131 | + }, |
| 132 | + contextUrl: "https://example.com:9090/hello/swagger.json" |
| 133 | + }) |
| 134 | + |
| 135 | + expect(res).toEqual("https://example.com:9090") |
| 136 | + }) |
| 137 | +}) |
0 commit comments