File tree Expand file tree Collapse file tree 3 files changed +87
-0
lines changed
spec/foobara/command_connectors_http Expand file tree Collapse file tree 3 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 1+ RSpec . describe Foobara ::CommandConnectors ::Http ::MoveAttributeToHeader do
2+ let ( :command_class ) do
3+ stub_class ( :SomeCommand , Foobara ::Command ) do
4+ result foo : :string , bar : :string
5+
6+ def execute
7+ { foo : "foo value" , bar : "bar value" }
8+ end
9+ end
10+ end
11+
12+ let ( :response_mutator ) do
13+ described_class . for ( :foo , "foo_header" )
14+ end
15+
16+ let ( :command_connector ) { Foobara ::CommandConnectors ::Http . new }
17+ let ( :response ) { command_connector . run ( path : "/run/#{ command_class . full_command_name } " ) }
18+
19+ before do
20+ command_connector . connect ( command_class , response_mutators : response_mutator )
21+ end
22+
23+ it "moves the attribute to a header" do
24+ expect ( response . status ) . to eq ( 200 )
25+ expect ( JSON . parse ( response . body ) ) . to eq ( "bar" => "bar value" )
26+
27+ expect ( response . headers [ "foo_header" ] ) . to eq ( "foo value" )
28+ end
29+
30+ it "makes changes in the manifest" do
31+ manifest = command_connector . foobara_manifest
32+
33+ expect ( manifest [ :command ] [ :SomeCommand ] [ :result_type ] ) . to eq (
34+ type : :attributes ,
35+ element_type_declarations : {
36+ bar : { type : :string }
37+ }
38+ )
39+ end
40+ end
Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ def cookies
1919 def add_cookie ( cookie_name , cookie_value , cookie_opts )
2020 cookies << Cookie . new ( cookie_name , cookie_value , **cookie_opts )
2121 end
22+
23+ def add_header ( header_name , header_value )
24+ self . headers ||= { }
25+ headers [ header_name ] = header_value
26+ end
2227 end
2328 end
2429 end
Original file line number Diff line number Diff line change 1+ module Foobara
2+ module CommandConnectors
3+ class Http < CommandConnector
4+ class MoveAttributeToHeader < ResponseMutator
5+ class << self
6+ attr_accessor :attribute_name , :header_name
7+
8+ def for ( attribute_name , header_name = attribute_name )
9+ subclass = Class . new ( self )
10+
11+ subclass . attribute_name = attribute_name
12+ subclass . header_name = header_name
13+
14+ subclass
15+ end
16+ end
17+
18+ attr_writer :attribute_name , :header_name
19+
20+ def result_type_from ( result_type )
21+ new_declaration = TypeDeclarations ::Attributes . reject ( result_type . declaration_data , attribute_name )
22+ Domain . current . foobara_type_from_declaration ( new_declaration )
23+ end
24+
25+ def mutate ( response )
26+ if response . command . success?
27+ header_value = response . body . delete ( attribute_name )
28+ response . add_header ( header_name , header_value )
29+ end
30+ end
31+
32+ def attribute_name
33+ @attribute_name ||= self . class . attribute_name
34+ end
35+
36+ def header_name
37+ @header_name ||= self . class . header_name
38+ end
39+ end
40+ end
41+ end
42+ end
You can’t perform that action at this time.
0 commit comments