-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_imap_fetch.rb
More file actions
171 lines (160 loc) · 6.92 KB
/
test_imap_fetch.rb
File metadata and controls
171 lines (160 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true
require "net/imap"
require "test/unit"
require_relative "fake_server"
class IMAPFetchTest < Net::IMAP::TestCase
include Net::IMAP::FakeServer::TestHelper
test "argument errors" do
with_fake_server select: "inbox" do |_, imap|
assert_raise_with_message(ArgumentError, /\Apartial.*uid_fetch/) do
imap.fetch(1, "FAST", partial: 1..10)
end
assert_raise_with_message(ArgumentError, /\Avanished.*uid_fetch/) do
imap.fetch(1, "FAST", changedsince: 1234, vanished: true)
end
assert_raise_with_message(ArgumentError, /\Avanished.*changedsince/) do
imap.uid_fetch(1, "FAST", vanished: true)
end
end
end
test "#fetch with FETCH responses" do
with_fake_server select: "inbox" do |server, imap|
server.on("FETCH") do |resp|
resp.untagged("123 FETCH (UID 1111 FLAGS (\\Seen $MDNSent))")
resp.untagged("456 FETCH (UID 4444 FLAGS (\\Seen \\Answered))")
resp.untagged("789 FETCH (UID 7777 FLAGS ())")
resp.done_ok
end
fetched = imap.fetch [123, 456, 789], %w[UID FLAGS]
assert_equal 3, fetched.size
assert_instance_of Net::IMAP::FetchData, fetched[0]
assert_instance_of Net::IMAP::FetchData, fetched[1]
assert_instance_of Net::IMAP::FetchData, fetched[2]
assert_equal 123, fetched[0].seqno
assert_equal 456, fetched[1].seqno
assert_equal 789, fetched[2].seqno
assert_equal 1111, fetched[0].uid
assert_equal 4444, fetched[1].uid
assert_equal 7777, fetched[2].uid
assert_equal [:Seen, "$MDNSent"], fetched[0].flags
assert_equal [:Seen, :Answered], fetched[1].flags
assert_equal [], fetched[2].flags
assert_equal("RUBY0002 FETCH 123,456,789 (UID FLAGS)",
server.commands.pop.raw.strip)
end
end
test "#uid_fetch with UIDFETCH responses" do
with_fake_server select: "inbox" do |server, imap|
server.on("UID FETCH") do |resp|
resp.untagged("1111 UIDFETCH (FLAGS (\\Seen $MDNSent))")
resp.untagged("4444 UIDFETCH (FLAGS (\\Seen \\Answered))")
resp.untagged("7777 UIDFETCH (FLAGS ())")
resp.done_ok
end
fetched = imap.uid_fetch [123, 456, 789], %w[FLAGS]
assert_equal 3, fetched.size
assert_instance_of Net::IMAP::UIDFetchData, fetched[0]
assert_instance_of Net::IMAP::UIDFetchData, fetched[1]
assert_instance_of Net::IMAP::UIDFetchData, fetched[2]
assert_equal 1111, fetched[0].uid
assert_equal 4444, fetched[1].uid
assert_equal 7777, fetched[2].uid
assert_equal [:Seen, "$MDNSent"], fetched[0].flags
assert_equal [:Seen, :Answered], fetched[1].flags
assert_equal [], fetched[2].flags
assert_equal("RUBY0002 UID FETCH 123,456,789 (FLAGS)",
server.commands.pop.raw.strip)
end
end
test "#fetch with changedsince" do
with_fake_server select: "inbox" do |server, imap|
server.on("FETCH", &:done_ok)
fetched = imap.fetch 1..-1, %w[FLAGS], changedsince: 12345
assert_empty fetched
assert_equal("RUBY0002 FETCH 1:* (FLAGS) (CHANGEDSINCE 12345)",
server.commands.pop.raw.strip)
end
end
test "#uid_fetch with changedsince" do
with_fake_server select: "inbox" do |server, imap|
server.on("UID FETCH", &:done_ok)
fetched = imap.uid_fetch 1..-1, %w[FLAGS], changedsince: 12345
assert_empty fetched
assert_equal("RUBY0002 UID FETCH 1:* (FLAGS) (CHANGEDSINCE 12345)",
server.commands.pop.raw.strip)
end
end
test "#uid_fetch with partial" do
with_fake_server select: "inbox" do |server, imap|
server.on("UID FETCH", &:done_ok)
imap.uid_fetch 1.., "FAST", partial: 1..500
assert_equal("RUBY0002 UID FETCH 1:* FAST (PARTIAL 1:500)",
server.commands.pop.raw.strip)
imap.uid_fetch 1.., "FAST", partial: 1...501
assert_equal("RUBY0003 UID FETCH 1:* FAST (PARTIAL 1:500)",
server.commands.pop.raw.strip)
imap.uid_fetch 1.., "FAST", partial: -500..-1
assert_equal("RUBY0004 UID FETCH 1:* FAST (PARTIAL -500:-1)",
server.commands.pop.raw.strip)
imap.uid_fetch 1.., "FAST", partial: -500...-1
assert_equal("RUBY0005 UID FETCH 1:* FAST (PARTIAL -500:-2)",
server.commands.pop.raw.strip)
imap.uid_fetch 1.., "FAST", partial: 1..20, changedsince: 1234
assert_equal("RUBY0006 UID FETCH 1:* FAST (PARTIAL 1:20 CHANGEDSINCE 1234)",
server.commands.pop.raw.strip)
end
end
test "#uid_fetch with changedsince and vanished" do
with_fake_server select: "inbox" do |server, imap|
server.on("UID FETCH") do |resp|
resp.untagged "VANISHED (EARLIER) 300:310,405,411"
resp.untagged "1 FETCH (UID 404 MODSEQ (65402) FLAGS (\\Seen))"
resp.untagged "2 FETCH (UID 406 MODSEQ (75403) FLAGS (\\Deleted))"
resp.untagged "4 FETCH (UID 408 MODSEQ (29738) " \
"FLAGS ($NoJunk $AutoJunk $MDNSent))"
resp.done_ok
end
# vanished: true changes the output to begin with VanishedData
vanished, *fetched = imap.uid_fetch(300..500, %w[FLAGS],
changedsince: 12345, vanished: true)
assert_equal(
"RUBY0002 UID FETCH 300:500 (FLAGS) (CHANGEDSINCE 12345 VANISHED)",
server.commands.pop.raw.strip
)
assert_equal Net::IMAP::VanishedData["300:310,405,411", true], vanished
expected = [
[1, 404, 65402, %i[Seen]],
[2, 406, 75403, %i[Deleted]],
[4, 408, 29738, %w[$NoJunk $AutoJunk $MDNSent]],
]
assert_equal expected.size, fetched.size
fetched.zip(expected).each do |fetch, (seqno, uid, modseq, flags)|
assert_instance_of Net::IMAP::FetchData, fetch
assert_equal seqno, fetch.seqno
assert_equal uid, fetch.uid
assert_equal modseq, fetch.modseq
assert_equal flags, fetch.flags
end
# without VANISHED
server.on("UID FETCH") do |resp|
resp.untagged "1 FETCH (UID 404 MODSEQ (65402) FLAGS (\\Seen))"
resp.untagged "2 FETCH (UID 406 MODSEQ (75403) FLAGS (\\Deleted))"
resp.untagged "4 FETCH (UID 408 MODSEQ (29738) " \
"FLAGS ($NoJunk $AutoJunk $MDNSent))"
resp.done_ok
end
vanished, *fetched = imap.uid_fetch(300..500, %w[FLAGS],
changedsince: 12345, vanished: true)
assert_equal(Net::IMAP::VanishedData[Net::IMAP::SequenceSet.empty, true],
vanished)
assert_equal expected.size, fetched.size
fetched.zip(expected).each do |fetch, (seqno, uid, modseq, flags)|
assert_instance_of Net::IMAP::FetchData, fetch
assert_equal seqno, fetch.seqno
assert_equal uid, fetch.uid
assert_equal modseq, fetch.modseq
assert_equal flags, fetch.flags
end
end
end
end