Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ml authored and ml committed Jun 29, 2015
1 parent 888ad8b commit 1e4e36f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pyswagger/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ def _prepare_obj(self, strict=True):
if hasattr(self.__root, 'schemes') and self.__root.schemes:
if len(self.__root.schemes) > 0:
self.__schemes = self.__root.schemes
else:
# extract schemes from the url to load spec
self.__schemes = [six.moves.urlparse(self.__url).schemes]

s.scan(root=self.__root, route=[Resolve()])
s.scan(root=self.__root, route=[PatchObject()])
Expand Down
1 change: 1 addition & 0 deletions pyswagger/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def _patch(self, opt={}):
def prepare(self, scheme='http', handle_files=True, encoding='utf-8'):
""" make this request ready for Clients
:param str scheme: scheme used in this request
:param bool handle_files: False to skip multipart/form-data encoding
:param str encoding: encoding for body content.
:rtype: SwaggerRequest
Expand Down
8 changes: 7 additions & 1 deletion pyswagger/scanner/v2_0/patch_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def _path_item(self, path, obj, app):
"""
k = jp_split(path)[-1] # key to the dict containing PathItem(s)
if isinstance(app.root, Swagger):
url = app.root.host + (app.root.basePath or '') + k
host = app.root.host
if not host:
# deduce host from url to load the Swagger spec
host = six.moves.urllib.parse.urlunparse(('',) + six.moves.urllib.parse.urlparse(app.url)[1:])
host = host[2:] if host[:2] == '//' else host

url = host + (app.root.basePath or '') + k
base_path = app.root.basePath
else:
url = None
Expand Down
22 changes: 22 additions & 0 deletions pyswagger/tests/data/v2_0/patch/no_host_schemes/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"swagger":"2.0",
"produces":[
"application/json"
],
"consumes":[
"application/json"
],
"paths":{
"/t1":{
"get":{
"responses":{
"default":{
"description":"void"
}
}
}
}
},
"definitions":{
}
}
45 changes: 44 additions & 1 deletion pyswagger/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from pyswagger import SwaggerApp
from pyswagger import SwaggerApp, utils
from .utils import get_test_data_folder
from pyswagger.spec.base import BaseObj
import pyswagger
import unittest
import httpretty
import os
import six


class SwaggerCoreTestCase(unittest.TestCase):
Expand Down Expand Up @@ -57,3 +58,45 @@ def test_load_from_url_without_file(self):
self.assertTrue(app.schemes, ['http'])
self.assertTrue(isinstance(app.root, BaseObj))

def test_no_host_basePath(self):
""" test case for swagger.json without
'host' and 'basePath' defined
"""
path = get_test_data_folder(
version='2.0',
which=os.path.join('patch', 'no_host_schemes')
)
fu = utils.normalize_url(path) # file uri version of path

# load swagger.json from a file path
app = SwaggerApp.create(path)
req, _ = app.s('t1').get()
self.assertEqual(req.url, path+'/t1')
self.assertEqual(req.schemes, ['file'])
req.prepare(scheme='file', handle_files=False)
self.assertEqual(req.url, fu+'/t1')

# load swagger.json from a file uri
self.assertNotEqual(six.moves.urllib.parse.urlparse(fu).scheme, '')
app = SwaggerApp.create(fu)
req, _ = app.s('t1').get()
self.assertEqual(req.url, path+'/t1')
self.assertEqual(req.schemes, ['file'])
req.prepare(scheme='file', handle_files=False)
self.assertEqual(req.url, fu+'/t1')

# load swagger.json from a remote uri
def _hook(url):
# no matter what url, return the path of local swagger.json
return fu

url = 'test.com/api/v1'
app = SwaggerApp.load('https://'+url, url_load_hook=_hook)
app.prepare()
# try to make a SwaggerRequest and verify its url
req, _ = app.s('t1').get()
self.assertEqual(req.url, url+'/t1')
self.assertEqual(req.schemes, ['https'])
req.prepare(scheme='https', handle_files=False)
self.assertEqual(req.url, 'https://'+url+'/t1')

0 comments on commit 1e4e36f

Please sign in to comment.