11import asyncio
22import json
3+ import pathlib
34import pytest
45import yaml
56from os .path import join , dirname , abspath
@@ -101,7 +102,8 @@ def test_swagger_file_url(test_client, loop):
101102
102103 app = web .Application (loop = loop )
103104 setup_swagger (app ,
104- swagger_from_file = TESTS_PATH + "/data/example_swagger.yaml" )
105+ swagger_from_file = TESTS_PATH + "/data/example_swagger.yaml" ,
106+ json_only = True )
105107
106108 client = yield from test_client (app )
107109 resp1 = yield from client .get ('/api/doc/swagger.json' )
@@ -117,7 +119,7 @@ def test_swagger_file_url(test_client, loop):
117119def test_partial_swagger_file (test_client , loop ):
118120 app = web .Application (loop = loop )
119121 app .router .add_route ('GET' , "/ping-partial" , ping_partial )
120- setup_swagger (app )
122+ setup_swagger (app , json_only = True )
121123
122124 client = yield from test_client (app )
123125 resp1 = yield from client .get ('/api/doc/swagger.json' )
@@ -137,7 +139,8 @@ def test_custom_swagger(test_client, loop):
137139 description = description ,
138140 title = "Test Custom Title" ,
139141 api_version = "1.0.0" ,
140- contact = "my.custom.contact@example.com" )
142+ contact = "my.custom.contact@example.com" ,
143+ json_only = True )
141144
142145 client = yield from test_client (app )
143146 resp1 = yield from client .get ('/api/v1/doc/swagger.json' )
@@ -159,7 +162,8 @@ def test_swagger_home_decorator(test_client, loop):
159162 title = "Test Custom Title" ,
160163 api_version = "1.0.0" ,
161164 contact = "my.custom.contact@example.com" ,
162- swagger_home_decor = lambda x : x )
165+ swagger_home_decor = lambda x : x ,
166+ json_only = True )
163167
164168 client = yield from test_client (app )
165169 resp1 = yield from client .get ('/api/v1/doc/swagger.json' )
@@ -181,7 +185,8 @@ def test_swagger_def_decorator(test_client, loop):
181185 title = "Test Custom Title" ,
182186 api_version = "1.0.0" ,
183187 contact = "my.custom.contact@example.com" ,
184- swagger_def_decor = lambda x : x )
188+ swagger_def_decor = lambda x : x ,
189+ json_only = True )
185190
186191 client = yield from test_client (app )
187192 resp1 = yield from client .get ('/api/v1/doc/swagger.json' )
@@ -205,7 +210,8 @@ def test_swagger_info(test_client, loop, swagger_info):
205210 description = "Test Custom Swagger"
206211 setup_swagger (app ,
207212 swagger_url = "/api/v1/doc" ,
208- swagger_info = swagger_info )
213+ swagger_info = swagger_info ,
214+ json_only = True )
209215
210216 client = yield from test_client (app )
211217 resp1 = yield from client .get ('/api/v1/doc/swagger.json' )
@@ -221,7 +227,7 @@ def test_swagger_info(test_client, loop, swagger_info):
221227def test_undocumented_fn (test_client , loop ):
222228 app = web .Application (loop = loop )
223229 app .router .add_route ('GET' , "/undoc_ping" , undoc_ping )
224- setup_swagger (app )
230+ setup_swagger (app , json_only = True )
225231 client = yield from test_client (app )
226232 resp = yield from client .get ('/undoc_ping' )
227233 assert resp .status == 200
@@ -235,7 +241,7 @@ def test_undocumented_fn(test_client, loop):
235241def test_class_view (test_client , loop ):
236242 app = web .Application (loop = loop )
237243 app .router .add_route ('*' , "/class_view" , ClassView )
238- setup_swagger (app )
244+ setup_swagger (app , json_only = True )
239245
240246 client = yield from test_client (app )
241247 # GET
@@ -277,7 +283,7 @@ def test_class_view(test_client, loop):
277283def test_sub_app (test_client , loop ):
278284 sub_app = web .Application (loop = loop )
279285 sub_app .router .add_route ('*' , "/class_view" , ClassView )
280- setup_swagger (sub_app , api_base_url = '/sub_app' )
286+ setup_swagger (sub_app , api_base_url = '/sub_app' , json_only = True )
281287 app = web .Application (loop = loop )
282288 app .add_subapp (prefix = '/sub_app' , subapp = sub_app )
283289
@@ -294,3 +300,22 @@ def test_sub_app(test_client, loop):
294300 assert "/class_view" in result ['paths' ]
295301 assert "get" in result ['paths' ]["/class_view" ]
296302 assert "post" in result ['paths' ]["/class_view" ]
303+
304+
305+ @asyncio .coroutine
306+ def test_swagger_defined_paths (test_client , loop ):
307+ app1 = web .Application (loop = loop )
308+ setup_swagger (app1 , json_only = True )
309+ urls1 = [r .get_info () for r in app1 .router .resources ()]
310+ assert urls1 == [{'path' : '/api/doc/swagger.json' }]
311+
312+ app2 = web .Application (loop = loop )
313+ setup_swagger (app2 )
314+ urls2 = [r .get_info () for r in app2 .router .resources ()]
315+ assert urls2 == [
316+ {'path' : '/api/doc/swagger.json' },
317+ {'path' : '/api/doc' },
318+ {'path' : '/api/doc/' },
319+ {'directory' : pathlib .Path ('aiohttp_swagger/swagger_ui' ).absolute (),
320+ 'prefix' : '/api/doc/swagger_static' },
321+ ]
0 commit comments