Skip to content

Commit ac797bc

Browse files
authored
Merge pull request #46 from HoverHell/paramlocations
Request headers and cookies parameter parsing fix
2 parents d4ada7b + f8e977c commit ac797bc

File tree

7 files changed

+232
-18
lines changed

7 files changed

+232
-18
lines changed

openapi_core/validation/request/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class RequestParameters(dict):
88

9-
valid_locations = ['path', 'query', 'headers', 'cookies']
9+
valid_locations = ['path', 'query', 'header', 'cookie']
1010

1111
def __getitem__(self, location):
1212
self.validate_location(location)

openapi_core/wrappers/flask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def parameters(self):
3131
return {
3232
'path': self.request.view_args,
3333
'query': self.request.args,
34-
'headers': self.request.headers,
35-
'cookies': self.request.cookies,
34+
'header': self.request.headers,
35+
'cookie': self.request.cookies,
3636
}
3737

3838
@property

tests/integration/data/v3.0/petstore.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ paths:
7474
operationId: createPets
7575
tags:
7676
- pets
77+
parameters:
78+
- name: api_key
79+
in: header
80+
schema:
81+
type: integer
82+
format: int32
83+
required: true
84+
- name: user
85+
in: cookie
86+
schema:
87+
type: integer
88+
format: int32
89+
required: true
7790
requestBody:
7891
required: true
7992
content:

tests/integration/test_petstore.py

Lines changed: 165 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,29 @@ def test_post_birds(self, spec, spec_dict):
397397
}
398398
}
399399
data = json.dumps(data_json)
400+
headers = {
401+
'api_key': '12345',
402+
}
403+
cookies = {
404+
'user': '123',
405+
}
400406

401407
request = MockRequest(
402408
host_url, 'POST', '/pets',
403409
path_pattern=path_pattern, data=data,
410+
headers=headers, cookies=cookies,
404411
)
405412

406413
parameters = request.get_parameters(spec)
407414

408-
assert parameters == {}
415+
assert parameters == {
416+
'header': {
417+
'api_key': 12345,
418+
},
419+
'cookie': {
420+
'user': 123,
421+
},
422+
}
409423

410424
body = request.get_body(spec)
411425

@@ -443,15 +457,29 @@ def test_post_cats(self, spec, spec_dict):
443457
}
444458
}
445459
data = json.dumps(data_json)
460+
headers = {
461+
'api_key': '12345',
462+
}
463+
cookies = {
464+
'user': '123',
465+
}
446466

447467
request = MockRequest(
448468
host_url, 'POST', '/pets',
449469
path_pattern=path_pattern, data=data,
470+
headers=headers, cookies=cookies,
450471
)
451472

452473
parameters = request.get_parameters(spec)
453474

454-
assert parameters == {}
475+
assert parameters == {
476+
'header': {
477+
'api_key': 12345,
478+
},
479+
'cookie': {
480+
'user': 123,
481+
},
482+
}
455483

456484
body = request.get_body(spec)
457485

@@ -489,15 +517,29 @@ def test_post_cats_boolean_string(self, spec, spec_dict):
489517
}
490518
}
491519
data = json.dumps(data_json)
520+
headers = {
521+
'api_key': '12345',
522+
}
523+
cookies = {
524+
'user': '123',
525+
}
492526

493527
request = MockRequest(
494528
host_url, 'POST', '/pets',
495529
path_pattern=path_pattern, data=data,
530+
headers=headers, cookies=cookies,
496531
)
497532

498533
parameters = request.get_parameters(spec)
499534

500-
assert parameters == {}
535+
assert parameters == {
536+
'header': {
537+
'api_key': 12345,
538+
},
539+
'cookie': {
540+
'user': 123,
541+
},
542+
}
501543

502544
body = request.get_body(spec)
503545

@@ -523,15 +565,29 @@ def test_post_no_one_of_schema(self, spec, spec_dict):
523565
'alias': alias,
524566
}
525567
data = json.dumps(data_json)
568+
headers = {
569+
'api_key': '12345',
570+
}
571+
cookies = {
572+
'user': '123',
573+
}
526574

527575
request = MockRequest(
528576
host_url, 'POST', '/pets',
529577
path_pattern=path_pattern, data=data,
578+
headers=headers, cookies=cookies,
530579
)
531580

532581
parameters = request.get_parameters(spec)
533582

534-
assert parameters == {}
583+
assert parameters == {
584+
'header': {
585+
'api_key': 12345,
586+
},
587+
'cookie': {
588+
'user': 123,
589+
},
590+
}
535591

536592
with pytest.raises(NoOneOfSchema):
537593
request.get_body(spec)
@@ -548,15 +604,29 @@ def test_post_cats_only_required_body(self, spec, spec_dict):
548604
}
549605
}
550606
data = json.dumps(data_json)
607+
headers = {
608+
'api_key': '12345',
609+
}
610+
cookies = {
611+
'user': '123',
612+
}
551613

552614
request = MockRequest(
553615
host_url, 'POST', '/pets',
554616
path_pattern=path_pattern, data=data,
617+
headers=headers, cookies=cookies,
555618
)
556619

557620
parameters = request.get_parameters(spec)
558621

559-
assert parameters == {}
622+
assert parameters == {
623+
'header': {
624+
'api_key': 12345,
625+
},
626+
'cookie': {
627+
'user': 123,
628+
},
629+
}
560630

561631
body = request.get_body(spec)
562632

@@ -575,19 +645,101 @@ def test_post_pets_raises_invalid_mimetype(self, spec):
575645
'tag': 'cats',
576646
}
577647
data = json.dumps(data_json)
648+
headers = {
649+
'api_key': '12345',
650+
}
651+
cookies = {
652+
'user': '123',
653+
}
578654

579655
request = MockRequest(
580656
host_url, 'POST', '/pets',
581657
path_pattern=path_pattern, data=data, mimetype='text/html',
658+
headers=headers, cookies=cookies,
582659
)
583660

584661
parameters = request.get_parameters(spec)
585662

586-
assert parameters == {}
663+
assert parameters == {
664+
'header': {
665+
'api_key': 12345,
666+
},
667+
'cookie': {
668+
'user': 123,
669+
},
670+
}
587671

588672
with pytest.raises(InvalidContentType):
589673
request.get_body(spec)
590674

675+
def test_post_pets_missing_cookie(self, spec, spec_dict):
676+
host_url = 'http://petstore.swagger.io/v1'
677+
path_pattern = '/v1/pets'
678+
pet_name = 'Cat'
679+
pet_healthy = True
680+
data_json = {
681+
'name': pet_name,
682+
'ears': {
683+
'healthy': pet_healthy,
684+
}
685+
}
686+
data = json.dumps(data_json)
687+
headers = {
688+
'api_key': '12345',
689+
}
690+
691+
request = MockRequest(
692+
host_url, 'POST', '/pets',
693+
path_pattern=path_pattern, data=data,
694+
headers=headers,
695+
)
696+
697+
with pytest.raises(MissingRequiredParameter):
698+
request.get_parameters(spec)
699+
700+
body = request.get_body(spec)
701+
702+
schemas = spec_dict['components']['schemas']
703+
pet_model = schemas['PetCreate']['x-model']
704+
assert body.__class__.__name__ == pet_model
705+
assert body.name == pet_name
706+
assert not hasattr(body, 'tag')
707+
assert not hasattr(body, 'address')
708+
709+
def test_post_pets_missing_header(self, spec, spec_dict):
710+
host_url = 'http://petstore.swagger.io/v1'
711+
path_pattern = '/v1/pets'
712+
pet_name = 'Cat'
713+
pet_healthy = True
714+
data_json = {
715+
'name': pet_name,
716+
'ears': {
717+
'healthy': pet_healthy,
718+
}
719+
}
720+
data = json.dumps(data_json)
721+
cookies = {
722+
'user': '123',
723+
}
724+
725+
request = MockRequest(
726+
host_url, 'POST', '/pets',
727+
path_pattern=path_pattern, data=data,
728+
cookies=cookies,
729+
)
730+
731+
with pytest.raises(MissingRequiredParameter):
732+
request.get_parameters(spec)
733+
734+
body = request.get_body(spec)
735+
736+
schemas = spec_dict['components']['schemas']
737+
pet_model = schemas['PetCreate']['x-model']
738+
assert body.__class__.__name__ == pet_model
739+
assert body.name == pet_name
740+
assert not hasattr(body, 'tag')
741+
assert not hasattr(body, 'address')
742+
591743
def test_post_pets_raises_invalid_server_error(self, spec):
592744
host_url = 'http://flowerstore.swagger.io/v1'
593745
path_pattern = '/v1/pets'
@@ -596,10 +748,17 @@ def test_post_pets_raises_invalid_server_error(self, spec):
596748
'tag': 'cats',
597749
}
598750
data = json.dumps(data_json)
751+
headers = {
752+
'api_key': '12345',
753+
}
754+
cookies = {
755+
'user': '123',
756+
}
599757

600758
request = MockRequest(
601759
host_url, 'POST', '/pets',
602760
path_pattern=path_pattern, data=data, mimetype='text/html',
761+
headers=headers, cookies=cookies,
603762
)
604763

605764
with pytest.raises(InvalidServer):

0 commit comments

Comments
 (0)