@@ -1190,7 +1190,9 @@ class WebhookHandler < Goliath::API
puts "RESPONSE4: #{msg.inspect}"
end
- msg.from = others_num + '@' + ARGV[0]
+ # if message-failed, we already set msg.from
+ # moreover, we said `msg = msg.as_error`, and StanzaError
+ msg.from = others_num + '@' + ARGV[0] if msg.respond_to?(:from=)
SGXbwmsgsv2.write(msg)
# Emit event to messages stream
@@ -1202,7 +1204,7 @@ class WebhookHandler < Goliath::API
MessageEvent::In.new(
timestamp: jparams['time'],
from: jparams['from'],
- to: jparams['to'],
+ to: jparams['to'] << users_num,
owner: jparams['owner'],
bandwidth_id: jparams['id'],
body: jparams['text'].to_s,
@@ -1234,7 +1236,7 @@ class WebhookHandler < Goliath::API
puts 'Shutting down gateway due to exception 013: ' + e.message
SGXbwmsgsv2.shutdown
puts 'Gateway has terminated.'
- EM.stop
+ EM.stop unless ENV['ENV'] == 'test'
end
end
@@ -9,6 +9,7 @@ end
class ComponentTest < Minitest::Test
def setup
+ ARGV[0] = 'component'
SGXbwmsgsv2.instance_variable_set(:@written, [])
def SGXbwmsgsv2.write_to_stream(s)
@@ -582,25 +583,34 @@ class ComponentTest < Minitest::Test
em :test_passthrough_message_emits_to_stream
def invoke_webhook(payload)
- handler = WebhookHandler.new
- env = {
- "REQUEST_URI" => "/",
- "REQUEST_METHOD" => "POST",
- "params" => {"_json" => [payload]}
- }
- handler.instance_variable_set(:@env, env)
- def handler.params
- @env["params"]
- end
+ with_stubs([
+ [
+ SGXbwmsgsv2,
+ :write,
+ ->(data) { SGXbwmsgsv2.write_to_stream(data) }
+ ]
+ ]) do
+ handler = WebhookHandler.new
+ env = {
+ "REQUEST_URI" => "/",
+ "REQUEST_METHOD" => "POST",
+ "params" => {"_json" => [payload]}
+ }
+ handler.instance_variable_set(:@env, env)
+ def handler.params
+ @env["params"]
+ end
- EMPromise.resolve(nil).then {
- handler.response(env)
- }.sync
+ EMPromise.resolve(nil).then {
+ handler.response(env)
+ }.sync
+ end
end
def test_inbound_sms_emits_to_stream
payload = {
"type" => "message-received",
+ "to" => "+15550000000",
"message" => {
"id" => "bw-in-123",
"direction" => "in",
@@ -631,6 +641,7 @@ class ComponentTest < Minitest::Test
def test_inbound_mms_emits_to_stream_and_filters_smil
payload = {
"type" => "message-received",
+ "to" => "+15550000000",
"message" => {
"id" => "bw-mms-456",
"direction" => "in",
@@ -662,6 +673,7 @@ class ComponentTest < Minitest::Test
def test_message_delivered_emits_to_stream
payload = {
"type" => "message-delivered",
+ "to" => "+15550000000",
"message" => {
"id" => "bw-out-789",
"direction" => "out",
@@ -669,7 +681,7 @@ class ComponentTest < Minitest::Test
"from" => "+15550000000",
"to" => ["+15551234567"],
"time" => "2025-01-13T10:10:00Z",
- "tag" => "stanza-id-abc%20extra-data"
+ "tag" => "stanza-id-abc extra-data"
}
}
@@ -689,6 +701,7 @@ class ComponentTest < Minitest::Test
def test_message_failed_emits_to_stream
payload = {
"type" => "message-failed",
+ "to" => "+15551234567",
"message" => {
"id" => "bw-out-999",
"direction" => "out",
@@ -696,7 +709,7 @@ class ComponentTest < Minitest::Test
"from" => "+15550000000",
"to" => ["+15551234567"],
"time" => "2025-01-13T10:15:00Z",
- "tag" => "failed-stanza-xyz%20extra",
+ "tag" => "failed-stanza-xyz extra",
"errorCode" => 4720,
"description" => "Carrier rejected message"
}