Skip to content

Commit 4d038fb

Browse files
committed
🥅 Return empty array for missing server response
When the server fails to send a response for certain commands, we can workaround this by simply returning an empty array. This should work for `SEARCH`, `SORT`, `THREAD`, and `ENABLED`, each of which expects a single response. I did not do the same for `CAPABILITY` for two reasons: * unless written carefully, it could break the cache. * the server _must_ at least return `* CAPABILITY IMAP4rev1`, so a missing response may indicate a bigger problems.
1 parent 37a1344 commit 4d038fb

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lib/net/imap.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@ def enable(*capabilities)
21882188
.join(' ')
21892189
synchronize do
21902190
send_command("ENABLE #{capabilities}")
2191-
result = clear_responses("ENABLED").last
2191+
result = clear_responses("ENABLED").last || []
21922192
@utf8_strings ||= result.include? "UTF8=ACCEPT"
21932193
@utf8_strings ||= result.include? "IMAP4REV2"
21942194
result
@@ -2642,7 +2642,7 @@ def search_internal(cmd, keys, charset)
26422642
else
26432643
send_command(cmd, *keys)
26442644
end
2645-
clear_responses("SEARCH").last
2645+
clear_responses("SEARCH").last || []
26462646
end
26472647
end
26482648

@@ -2691,7 +2691,7 @@ def sort_internal(cmd, sort_keys, search_keys, charset)
26912691
normalize_searching_criteria(search_keys)
26922692
synchronize do
26932693
send_command(cmd, sort_keys, charset, *search_keys)
2694-
clear_responses("SORT").last
2694+
clear_responses("SORT").last || []
26952695
end
26962696
end
26972697

@@ -2704,7 +2704,7 @@ def thread_internal(cmd, algorithm, search_keys, charset)
27042704
normalize_searching_criteria(search_keys)
27052705
synchronize do
27062706
send_command(cmd, algorithm, charset, *search_keys)
2707-
clear_responses("THREAD").last
2707+
clear_responses("THREAD").last || []
27082708
end
27092709
end
27102710

test/net/imap/test_imap.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,47 @@ def test_unselect
11381138
end
11391139
end
11401140

1141+
test("missing server ENABLED response") do
1142+
with_fake_server do |server, imap|
1143+
server.on "ENABLE", &:done_ok
1144+
enabled = imap.enable "foo", "bar", "baz"
1145+
assert_equal [], enabled
1146+
end
1147+
end
1148+
1149+
test("missing server SEARCH response") do
1150+
with_fake_server do |server, imap|
1151+
server.on "SEARCH", &:done_ok
1152+
server.on "UID SEARCH", &:done_ok
1153+
found = imap.search ["subject", "hello"]
1154+
assert_equal [], found
1155+
found = imap.uid_search ["subject", "hello"]
1156+
assert_equal [], found
1157+
end
1158+
end
1159+
1160+
test("missing server SORT response") do
1161+
with_fake_server do |server, imap|
1162+
server.on "SORT", &:done_ok
1163+
server.on "UID SORT", &:done_ok
1164+
found = imap.sort ["INTERNALDATE"], ["subject", "hello"], "UTF-8"
1165+
assert_equal [], found
1166+
found = imap.uid_sort ["INTERNALDATE"], ["subject", "hello"], "UTF-8"
1167+
assert_equal [], found
1168+
end
1169+
end
1170+
1171+
test("missing server THREAD response") do
1172+
with_fake_server do |server, imap|
1173+
server.on "THREAD", &:done_ok
1174+
server.on "UID THREAD", &:done_ok
1175+
found = imap.thread "REFERENCES", ["subject", "hello"], "UTF-8"
1176+
assert_equal [], found
1177+
found = imap.uid_thread "REFERENCES", ["subject", "hello"], "UTF-8"
1178+
assert_equal [], found
1179+
end
1180+
end
1181+
11411182
private
11421183

11431184
def imaps_test(timeout: 10)

0 commit comments

Comments
 (0)