From a28071a75e5fbc426b9b481f5bb29a20659b82d8 Mon Sep 17 00:00:00 2001 From: Zachary Schulz Date: Tue, 21 Jul 2020 23:56:51 -0400 Subject: [PATCH] add request schema to fixture config --- lib/api_sim/app_builder.rb | 4 +++- spec/fixtures/path/to/response/POST.json.erb | 21 ++++++++++++++++++++ spec/http_sim_spec.rb | 11 ++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/path/to/response/POST.json.erb diff --git a/lib/api_sim/app_builder.rb b/lib/api_sim/app_builder.rb index 52b63ec..01851b2 100644 --- a/lib/api_sim/app_builder.rb +++ b/lib/api_sim/app_builder.rb @@ -65,12 +65,14 @@ def configure_fixture_directory(dir) Dir[File.join(dir, "**/*.json.erb")].each do |path| endpoint_match = path.match(%r{#{dir}([/\w+\_\-]+)/(GET|POST|PATCH|OPTIONS|HEAD|PUT|DELETE).json}) config = JSON.parse(File.read(path)) + request_schema = config['request_schema'].to_json unless config['request_schema'].nil? configure_endpoint endpoint_match[2], endpoint_match[1], config['body'].to_json, config['status'], config['headers'], - config['schema'].to_json + config['schema'].to_json, + request_schema: request_schema end end diff --git a/spec/fixtures/path/to/response/POST.json.erb b/spec/fixtures/path/to/response/POST.json.erb new file mode 100644 index 0000000..cc89669 --- /dev/null +++ b/spec/fixtures/path/to/response/POST.json.erb @@ -0,0 +1,21 @@ +{ + "status": 201, + "headers": { + "content-type": "application/json" + }, + "body": { + "id": 1, + "name": "Sammy" + }, + "request_schema": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "string" + } + } + } +} diff --git a/spec/http_sim_spec.rb b/spec/http_sim_spec.rb index 3e07582..4a41e61 100644 --- a/spec/http_sim_spec.rb +++ b/spec/http_sim_spec.rb @@ -345,6 +345,17 @@ def app expect(response_body['id']).to eq 1 end + it 'validates json from fixture configuration' do + response = make_request_to 'POST', '/path/to/response', {'name': 'mom'}.to_json, 'application/json' + + expect(response).to be_created + + response = make_request_to 'POST', '/path/to/response', {'not_name': 'mom'}.to_json, 'application/json' + + expect(response.status).to eq(498) + + end + it 'records the incoming Content-Type' do make_request_to 'POST', '/endpoint', {'hi': 'mom'}.to_json, 'application/foo'