Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
postmon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Matheus Horstmann
postmon
Commits
1f26e46a
Commit
1f26e46a
authored
11 years ago
by
Alê Borba
Browse files
Options
Downloads
Plain Diff
Merge pull request #66 from cauethenorio/jsonp
Adiciona suporte à requisições JSONP
parents
2798b110
2c4b8e4f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
PostmonServer.py
+17
-2
17 additions, 2 deletions
PostmonServer.py
test/postmon_test.py
+23
-0
23 additions, 0 deletions
test/postmon_test.py
with
40 additions
and
2 deletions
PostmonServer.py
+
17
−
2
View file @
1f26e46a
...
...
@@ -7,6 +7,7 @@ from correios import Correios
from
database
import
MongoDb
as
Database
app_v1
=
bottle
.
Bottle
()
jsonp_query_key
=
'
callback
'
def
expired
(
record_date
):
from
datetime
import
datetime
,
timedelta
...
...
@@ -26,6 +27,20 @@ def _get_info_from_source(cep):
return
info
def
format_result
(
result
):
# checa se foi solicitada resposta em JSONP
js_func_name
=
bottle
.
request
.
query
.
get
(
jsonp_query_key
)
if
js_func_name
:
# se a resposta vai ser JSONP, o content type deve ser js e seu
# conteudo deve ser JSON
response
.
content_type
=
'
application/javascript
'
result
=
json
.
dumps
(
result
)
result
=
'
%s(%s);
'
%
(
js_func_name
,
result
)
return
result
@route
(
'
/cep/<cep:re:\d{5}-?\d{3}>
'
)
@app_v1.route
(
'
/cep/<cep:re:\d{5}-?\d{3}>
'
)
def
verifica_cep
(
cep
):
...
...
@@ -52,7 +67,7 @@ def verifica_cep(cep):
if
result
:
response
.
headers
[
'
Cache-Control
'
]
=
'
public, max-age=2592000
'
return
result
return
format_result
(
result
)
else
:
response
.
status
=
'
404 O CEP %s informado nao pode ser localizado
'
%
cep
return
...
...
@@ -81,7 +96,7 @@ def track_pack(provider, track):
resposta
[
'
codigo
'
]
=
track
resposta
[
'
historico
'
]
=
result
return
json
.
dumps
(
resposta
)
return
format_result
(
resposta
)
except
AttributeError
:
response
.
status
=
'
404 O pacote %s informado nao pode ser localizado
'
%
track
...
...
This diff is collapsed.
Click to expand it.
test/postmon_test.py
+
23
−
0
View file @
1f26e46a
# encoding: utf-8
import
json
import
re
import
unittest
import
webtest
...
...
@@ -132,6 +134,27 @@ class PostmonWebTest(unittest.TestCase, PostmonBaseTest):
self
.
assertNotIn
(
'
v_date
'
,
result
)
class
PostmonWebJSONPTest
(
PostmonWebTest
):
'''
Teste de requisições JSONP no servidor do Postmon
'''
def
setUp
(
self
):
self
.
jsonp_query_key
=
PostmonServer
.
jsonp_query_key
self
.
jsonp_func_name
=
'
func_name
'
super
(
PostmonWebJSONPTest
,
self
).
setUp
()
def
get_cep
(
self
,
cep
):
response
=
self
.
app
.
get
(
'
/cep/%s?%s=%s
'
%
(
cep
,
self
.
jsonp_query_key
,
self
.
jsonp_func_name
))
regexp
=
re
.
compile
(
'
^%s\((.*)\);$
'
%
self
.
jsonp_func_name
)
json_data
=
re
.
findall
(
regexp
,
response
.
body
)[
0
]
return
json
.
loads
(
json_data
)
class
PostmonV1WebTest
(
PostmonWebTest
):
'''
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment