Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.

Commit f9d4cda

Browse files
authored
Use HTTP method from x-http-method-override header if present (#71)
1 parent fe8b3ac commit f9d4cda

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

endpoints_management/control/service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def _load_from_well_known_env():
7070
"rules": [{
7171
"selector": "allow-all.GET",
7272
"get": "{x}"
73+
}, {
74+
"selector": "allow-all.PATCH",
75+
"patch": "{x}"
7376
}, {
7477
"selector": "allow-all.POST",
7578
"post": "{x}"
@@ -79,6 +82,9 @@ def _load_from_well_known_env():
7982
"rules": [{
8083
"selector" : "allow-all.GET",
8184
"allowUnregisteredCalls" : true
85+
}, {
86+
"selector" : "allow-all.PATCH",
87+
"allowUnregisteredCalls" : true
8288
}, {
8389
"selector" : "allow-all.POST",
8490
"allowUnregisteredCalls" : true

endpoints_management/control/wsgi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ def _next_operation_uuid():
140140
return uuid.uuid4().hex
141141

142142

143+
def _request_method(environ):
144+
return environ.get(u'HTTP_X_HTTP_METHOD_OVERRIDE', environ[u'REQUEST_METHOD'])
145+
146+
143147
class EnvironmentMiddleware(object):
144148
"""A WSGI middleware that sets related variables in the environment.
145149
@@ -193,7 +197,7 @@ def __call__(self, environ, start_response):
193197
environ[self.METHOD_REGISTRY] = self._method_registry
194198
environ[self.REPORTING_RULES] = self._reporting_rules
195199
parsed_uri = urlparse.urlparse(wsgiref.util.request_uri(environ))
196-
http_method = environ.get(u'REQUEST_METHOD')
200+
http_method = _request_method(environ)
197201
method_info = self._method_registry.lookup(http_method, parsed_uri.path)
198202
if method_info:
199203
environ[self.METHOD_INFO] = method_info
@@ -264,7 +268,7 @@ def __call__(self, environ, start_response):
264268
latency_timer.start()
265269

266270
# Determine if the request can proceed
267-
http_method = environ.get(u'REQUEST_METHOD')
271+
http_method = _request_method(environ)
268272
parsed_uri = urlparse.urlparse(wsgiref.util.request_uri(environ))
269273
app_info = _AppInfo()
270274
# TODO: determine if any of the more complex ways of getting the request size

test/test_wsgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ def test_should_add_service_et_al_to_environment(self):
5959
expect(given.get(cls.REPORTING_RULES)).not_to(be_none)
6060
expect(given.get(cls.METHOD_INFO)).not_to(be_none)
6161

62+
def test_should_handle_method_override(self):
63+
cls = wsgi.EnvironmentMiddleware
64+
wrappee = _DummyWsgiApp()
65+
wanted_service = service.Loaders.SIMPLE.load()
66+
wrapped = cls(wrappee, wanted_service)
67+
68+
given = {
69+
u'wsgi.url_scheme': u'http',
70+
u'HTTP_HOST': u'localhost',
71+
u'REQUEST_METHOD': u'POST',
72+
u'HTTP_X_HTTP_METHOD_OVERRIDE': u'PATCH',
73+
}
74+
75+
wrapped(given, _dummy_start_response)
76+
assert given[cls.METHOD_INFO].selector == 'allow-all.PATCH'
77+
6278

6379
class TestMiddleware(unittest2.TestCase):
6480
PROJECT_ID = u'middleware'

0 commit comments

Comments
 (0)