Skip to content

Commit 0c96fb5

Browse files
committed
Add a SetInputFromHeader request mutator
1 parent 34f2f16 commit 0c96fb5

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.0.18] - 2025-03-29
2+
3+
- Implement SetInputsFromHeader request mutator and MoveAttributeToHeader response mutator
4+
15
## [0.0.17] - 2025-03-29
26

37
- Implement SetInputsFromCookie request mutator

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
foobara-http-command-connector (0.0.17)
4+
foobara-http-command-connector (0.0.18)
55
foobara (~> 0.0.88)
66

77
GEM
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
RSpec.describe Foobara::CommandConnectors::Http::SetInputFromHeader do
2+
let(:command_class) do
3+
stub_class(:SomeCommand, Foobara::Command) do
4+
inputs foo: :string, bar: :string
5+
result :duck
6+
7+
def execute
8+
inputs
9+
end
10+
end
11+
end
12+
13+
let(:request_mutator) do
14+
described_class.for(:foo, "foo_header")
15+
end
16+
17+
let(:command_connector) { Foobara::CommandConnectors::Http.new }
18+
let(:response) { command_connector.run(path:, query_string:, headers:) }
19+
let(:query_string) { "bar=barvalue" }
20+
let(:path) { "/run/#{command_class.full_command_name}" }
21+
let(:headers) do
22+
{ "foo_header" => "foovalue" }
23+
end
24+
25+
before do
26+
command_connector.connect(command_class, request_mutators: request_mutator)
27+
end
28+
29+
it "moves the header to an input" do
30+
expect(response.status).to eq(200)
31+
expect(JSON.parse(response.body)).to eq("bar" => "barvalue", "foo" => "foovalue")
32+
end
33+
34+
it "makes changes in the manifest" do
35+
manifest = command_connector.foobara_manifest
36+
37+
expect(manifest[:command][:SomeCommand][:inputs_type]).to eq(
38+
type: :attributes,
39+
element_type_declarations: {
40+
bar: { type: :string }
41+
}
42+
)
43+
end
44+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Foobara
2+
module CommandConnectors
3+
class Http < CommandConnector
4+
class SetInputFromHeader < RequestMutator
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+
header_name = header_name.to_s if header_name.is_a?(::Symbol)
12+
13+
subclass.attribute_name = attribute_name
14+
subclass.header_name = header_name
15+
16+
subclass
17+
end
18+
end
19+
20+
attr_writer :attribute_name, :header_name
21+
22+
def inputs_type_from(inputs_type)
23+
new_declaration = TypeDeclarations::Attributes.reject(inputs_type.declaration_data, attribute_name)
24+
Domain.current.foobara_type_from_declaration(new_declaration)
25+
end
26+
27+
def mutate(request)
28+
header_value = request.headers[header_name]
29+
request.inputs[attribute_name] = header_value
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

version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Foobara
22
module HttpCommandConnector
3-
VERSION = "0.0.17".freeze
3+
VERSION = "0.0.18".freeze
44

55
local_ruby_version = File.read("#{__dir__}/.ruby-version").chomp
66
local_ruby_version_minor = local_ruby_version[/\A(\d+\.\d+)\.\d+\z/, 1]

0 commit comments

Comments
 (0)