123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- require 'rails_helper'
- describe Agents::WebhookAgent do
- let(:agent) do
- _agent = Agents::WebhookAgent.new(:name => 'webhook',
- :options => { 'secret' => 'foobar', 'payload_path' => 'some_key' })
- _agent.user = users(:bob)
- _agent.save!
- _agent
- end
- let(:payload) { {'people' => [{ 'name' => 'bob' }, { 'name' => 'jon' }] } }
- describe 'receive_web_request' do
- it 'should create event if secret matches' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- agent.options['event_headers'] = 'Accept,X-Hello-World'
- agent.options['event_headers_key'] = 'X-HTTP-HEADERS'
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- expect(Event.last.payload).to eq( {"people"=>[{"name"=>"bob"}, {"name"=>"jon"}], "X-HTTP-HEADERS"=>{"Accept"=>"application/xml", "X-Hello-World"=>"Hello Huginn"}})
- end
- it 'should be able to create multiple events when given an array' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- agent.options['payload_path'] = 'some_key.people'
- agent.options['event_headers'] = 'Accept,X-Hello-World'
- agent.options['event_headers_key'] = 'X-HTTP-HEADERS'
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(2)
- expect(out).to eq(['Event Created', 201])
- expect(Event.last.payload).to eq({"name"=>"jon", "X-HTTP-HEADERS"=>{"Accept"=>"application/xml", "X-Hello-World"=>"Hello Huginn"}})
- end
- it 'should not create event if secrets do not match' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'bazbat' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- agent.options['event_headers'] = 'Accept,X-Hello-World'
- agent.options['event_headers_key'] = 'X-HTTP-HEADERS'
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Not Authorized', 401])
- end
- it 'should respond with customized response message if configured with `response` option' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['response'] = 'That Worked'
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['That Worked', 201])
- # Empty string is a valid response
- agent.options['response'] = ''
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['', 201])
- end
- it 'should respond with interpolated response message if configured with `response` option' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['response'] = '{{some_key.people[1].name}}'
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['jon', 201])
- end
- it 'should respond with custom response header if configured with `response_headers` option' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['response_headers'] = {"X-My-Custom-Header" => 'hello'}
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 201, "text/plain", {"X-My-Custom-Header" => 'hello'}])
- end
- it 'should respond with `Event Created` if the response option is nil or missing' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['response'] = nil
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 201])
- agent.options.delete('response')
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 201])
- end
- it 'should respond with customized response code if configured with `code` option' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['code'] = '200'
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 200])
- end
- it 'should respond with `201` if the code option is empty, nil or missing' do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['code'] = ''
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 201])
- agent.options['code'] = nil
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 201])
- agent.options.delete('code')
- out = agent.receive_web_request(webpayload)
- expect(out).to eq(['Event Created', 201])
- end
- describe "receiving events" do
- context "default settings" do
- it "should not accept GET" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "GET",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use POST requests only', 401])
- end
- it "should accept POST" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- end
- context "accepting get and post" do
- before { agent.options['verbs'] = 'get,post' }
- it "should accept GET" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "GET",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should accept POST" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should not accept PUT" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "PUT",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use GET/POST requests only', 401])
- end
- end
- context "accepting only get" do
- before { agent.options['verbs'] = 'get' }
- it "should accept GET" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "GET",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should not accept POST" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use GET requests only', 401])
- end
- end
- context "accepting only post" do
- before { agent.options['verbs'] = 'post' }
- it "should not accept GET" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "GET",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use POST requests only', 401])
- end
- it "should accept POST" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- end
- context "accepting only put" do
- before { agent.options['verbs'] = 'put' }
- it "should accept PUT" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "PUT",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should not accept GET" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "GET",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use PUT requests only', 401])
- end
- it "should not accept POST" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use PUT requests only', 401])
- end
- end
- context "flaky content with commas" do
- before { agent.options['verbs'] = ',, PUT,POST, gEt , ,' }
- it "should accept PUT" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "PUT",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should accept GET" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "GET",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should accept POST" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- end
- it "should not accept DELETE" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "DELETE",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(0)
- expect(out).to eq(['Please use PUT/POST/GET requests only', 401])
- end
- end
- context "with reCAPTCHA" do
- it "should not check a reCAPTCHA response unless recaptcha_secret is set" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- checked = false
- out = nil
- stub_request(:any, /verify/).to_return { |request|
- checked = true
- { status: 200, body: '{"success":false}' }
- }
- expect {
- out= agent.receive_web_request(webpayload)
- }.not_to change { checked }
- expect(out).to eq(["Event Created", 201])
- end
- it "should reject a request if recaptcha_secret is set but g-recaptcha-response is not given" do
- agent.options['recaptcha_secret'] = 'supersupersecret'
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- checked = false
- out = nil
- stub_request(:any, /verify/).to_return { |request|
- checked = true
- { status: 200, body: '{"success":false}' }
- }
- expect {
- out = agent.receive_web_request(webpayload)
- }.not_to change { checked }
- expect(out).to eq(["Not Authorized", 401])
- end
- it "should reject a request if recaptcha_secret is set and g-recaptcha-response given is not verified" do
- agent.options['recaptcha_secret'] = 'supersupersecret'
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload, 'g-recaptcha-response' => 'somevalue' },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- checked = false
- out = nil
- stub_request(:any, /verify/).to_return { |request|
- checked = true
- { status: 200, body: '{"success":false}' }
- }
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { checked }
- expect(out).to eq(["Not Authorized", 401])
- end
- it "should accept a request if recaptcha_secret is set and g-recaptcha-response given is verified" do
- agent.options['payload_path'] = '.'
- agent.options['recaptcha_secret'] = 'supersupersecret'
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => payload.merge({ 'g-recaptcha-response' => 'somevalue' }),
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- checked = false
- out = nil
- stub_request(:any, /verify/).to_return { |request|
- checked = true
- { status: 200, body: '{"success":true}' }
- }
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { checked }
- expect(out).to eq(["Event Created", 201])
- expect(Event.last.payload).to eq(payload)
- end
- it "should accept a request if recaptcha_secret is set and g-recaptcha-response given is verified and
- reCAPTCHA v3 score is above score_treshold" do
- agent.options['payload_path'] = '.'
- agent.options['recaptcha_secret'] = 'supersupersecret'
- agent.options['score_threshold'] = 0.5
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => payload.merge({ 'g-recaptcha-response' => 'somevalue' }),
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- checked = false
- out = nil
- stub_request(:any, /verify/).to_return { |request|
- checked = true
- { status: 200, body: '{"success":true, "score":0.9}' }
- }
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { checked }
- expect(out).to eq(["Event Created", 201])
- expect(Event.last.payload).to eq(payload)
- end
- it "should reject a request if recaptcha_secret is set and g-recaptcha-response given is verified and
- reCAPTCHA v3 score is below score_treshold" do
- agent.options['payload_path'] = '.'
- agent.options['recaptcha_secret'] = 'supersupersecret'
- agent.options['score_threshold'] = 0.5
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => payload.merge({ 'g-recaptcha-response' => 'somevalue' }),
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- checked = false
- out = nil
- stub_request(:any, /verify/).to_return { |request|
- checked = true
- { status: 200, body: '{"success":true, "score":0.1}' }
- }
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { checked }
- expect(out).to eq(["Not Authorized", 401])
- end
- end
- end
- context "with headers" do
- it "should not pass any headers if event_headers_key is not set" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['event_headers'] = 'Accept,X-Hello-World'
- agent.options['event_headers_key'] = ''
- out = nil
- expect {
- out = agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- expect(Event.last.payload).to eq(payload)
- end
- it "should pass selected headers specified in event_headers_key" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['event_headers'] = 'X-Hello-World'
- agent.options['event_headers_key'] = 'X-HTTP-HEADERS'
- out = nil
- expect {
- out= agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- expect(Event.last.payload).to eq({"people"=>[{"name"=>"bob"}, {"name"=>"jon"}], "X-HTTP-HEADERS"=>{"X-Hello-World"=>"Hello Huginn"}})
- end
- it "should pass empty event_headers_key if none of the headers exist" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['event_headers'] = 'x-hello-world1'
- agent.options['event_headers_key'] = 'X-HTTP-HEADERS'
- out = nil
- expect {
- out= agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- expect(Event.last.payload).to eq({"people"=>[{"name"=>"bob"}, {"name"=>"jon"}], "X-HTTP-HEADERS"=>{}})
- end
- it "should pass empty event_headers_key if event_headers is empty" do
- webpayload = ActionDispatch::Request.new({
- 'action_dispatch.request.request_parameters' => { 'some_key' => payload },
- 'action_dispatch.request.path_parameters' => { secret: 'foobar' },
- 'REQUEST_METHOD' => "POST",
- 'HTTP_ACCEPT' => 'application/xml',
- 'HTTP_X_HELLO_WORLD' => "Hello Huginn"
- })
- agent.options['event_headers'] = ''
- agent.options['event_headers_key'] = 'X-HTTP-HEADERS'
- out = nil
- expect {
- out= agent.receive_web_request(webpayload)
- }.to change { Event.count }.by(1)
- expect(out).to eq(['Event Created', 201])
- expect(Event.last.payload).to eq({"people"=>[{"name"=>"bob"}, {"name"=>"jon"}], "X-HTTP-HEADERS"=>{}})
- end
- end
- end
- end
|