fake_ftp 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/README.markdown +3 -1
- data/lib/fake_ftp/file.rb +10 -0
- data/lib/fake_ftp/server.rb +15 -8
- data/lib/fake_ftp/version.rb +1 -1
- data/lib/fake_ftp.rb +1 -0
- data/spec/models/fake_ftp/file_spec.rb +27 -0
- data/spec/models/fake_ftp/server_spec.rb +17 -33
- metadata +7 -4
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -35,13 +35,15 @@ Usage
|
|
35
35
|
ftp.put('some_file.txt')
|
36
36
|
ftp.close
|
37
37
|
|
38
|
+
server.files.should include('some_file.txt')
|
39
|
+
server.file('some_file.txt').bytes.should == 25
|
40
|
+
|
38
41
|
server.stop
|
39
42
|
|
40
43
|
TODO
|
41
44
|
----
|
42
45
|
|
43
46
|
* Active file upload
|
44
|
-
* Matchers
|
45
47
|
|
46
48
|
References
|
47
49
|
----------
|
data/lib/fake_ftp/server.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require 'socket'
|
2
|
-
require
|
2
|
+
require 'thread'
|
3
3
|
|
4
4
|
module FakeFtp
|
5
5
|
class Server
|
6
6
|
|
7
|
-
attr_accessor :
|
8
|
-
attr_reader :files
|
7
|
+
attr_accessor :port, :passive_port
|
9
8
|
|
10
9
|
CMDS = %w[acct cwd cdup pass pasv port pwd quit stor type user]
|
11
10
|
LNBK = "\r\n"
|
@@ -16,10 +15,16 @@ module FakeFtp
|
|
16
15
|
raise(Errno::EADDRINUSE, "#{port}") if is_running?
|
17
16
|
raise(Errno::EADDRINUSE, "#{passive_port}") if passive_port && is_running?(passive_port)
|
18
17
|
@connection = nil
|
19
|
-
@data = nil
|
20
18
|
@options = options
|
21
19
|
@files = []
|
22
|
-
|
20
|
+
end
|
21
|
+
|
22
|
+
def files
|
23
|
+
@files.map(&:name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def file(name)
|
27
|
+
@files.detect { |file| file.name == name}
|
23
28
|
end
|
24
29
|
|
25
30
|
def start
|
@@ -117,7 +122,7 @@ module FakeFtp
|
|
117
122
|
end
|
118
123
|
|
119
124
|
def _pwd(*args)
|
120
|
-
"257 \"
|
125
|
+
"257 \"/pub\" is current directory"
|
121
126
|
end
|
122
127
|
|
123
128
|
def _quit(*args)
|
@@ -127,11 +132,13 @@ module FakeFtp
|
|
127
132
|
end
|
128
133
|
|
129
134
|
def _stor(filename)
|
130
|
-
@files << File.basename(filename.to_s)
|
131
135
|
respond_with('125 Do it!')
|
132
136
|
|
133
137
|
data_client = @data_server.accept
|
134
|
-
|
138
|
+
data = data_client.recv(1024)
|
139
|
+
|
140
|
+
file = FakeFtp::File.new(::File.basename(filename.to_s), data.length)
|
141
|
+
@files << file
|
135
142
|
|
136
143
|
data_client.close
|
137
144
|
'226 Did it!'
|
data/lib/fake_ftp/version.rb
CHANGED
data/lib/fake_ftp.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "spec_helper.rb"
|
2
|
+
|
3
|
+
describe FakeFtp::File do
|
4
|
+
context 'attributes' do
|
5
|
+
before :each do
|
6
|
+
@file = FakeFtp::File.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has a name attribute" do
|
10
|
+
@file.name = "some name"
|
11
|
+
@file.name.should == "some name"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has a bytes attribute" do
|
15
|
+
@file.bytes = 87
|
16
|
+
@file.bytes.should == 87
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'setup' do
|
21
|
+
it "can be initialized with name and bytes" do
|
22
|
+
file = FakeFtp::File.new('filename', 104)
|
23
|
+
file.name.should == 'filename'
|
24
|
+
file.bytes.should == 104
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -4,15 +4,10 @@ require 'net/ftp'
|
|
4
4
|
describe FakeFtp::Server do
|
5
5
|
|
6
6
|
before :each do
|
7
|
-
@directory = File.expand_path("../fixtures/destination", File.dirname(__FILE__))
|
8
7
|
@text_filename = File.expand_path("../../fixtures/text_file.txt", File.dirname(__FILE__))
|
9
8
|
@bin_filename = File.expand_path("../../fixtures/invisible_bike.jpg", File.dirname(__FILE__))
|
10
9
|
end
|
11
10
|
|
12
|
-
after :each do
|
13
|
-
FileUtils.rm_rf(@directory+"/*")
|
14
|
-
end
|
15
|
-
|
16
11
|
context 'setup' do
|
17
12
|
it "starts a server on port n" do
|
18
13
|
server = FakeFtp::Server.new(21212)
|
@@ -60,28 +55,22 @@ describe FakeFtp::Server do
|
|
60
55
|
proc { FakeFtp::Server.new(21214, 21213) }.should raise_error(Errno::EADDRINUSE, "Address already in use - 21213")
|
61
56
|
server.stop
|
62
57
|
end
|
58
|
+
end
|
63
59
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
server
|
60
|
+
context 'files' do
|
61
|
+
before :each do
|
62
|
+
@file = FakeFtp::File.new('filename', 34)
|
63
|
+
@server = FakeFtp::Server.new(21212)
|
64
|
+
@server.instance_variable_set(:@files, [@file])
|
68
65
|
end
|
69
66
|
|
70
|
-
it "
|
71
|
-
server
|
72
|
-
server.directory.should == '/tmp'
|
67
|
+
it "returns filenames from :files" do
|
68
|
+
@server.files.should include('filename')
|
73
69
|
end
|
74
70
|
|
75
|
-
it "
|
76
|
-
|
77
|
-
Rails.stub!(:root).and_return('/somewhere')
|
78
|
-
server = FakeFtp::Server.new
|
79
|
-
server.directory.should == '/somewhere/tmp/ftp'
|
71
|
+
it "can be accessed with :file" do
|
72
|
+
@server.file('filename').should == @file
|
80
73
|
end
|
81
|
-
|
82
|
-
it "should clean up directory after itself"
|
83
|
-
|
84
|
-
it "should raise if attempting to delete a directory with contents other than its own"
|
85
74
|
end
|
86
75
|
|
87
76
|
context 'socket' do
|
@@ -234,25 +223,15 @@ describe FakeFtp::Server do
|
|
234
223
|
end
|
235
224
|
|
236
225
|
it "returns directory on PWD" do
|
237
|
-
@server.directory = @directory
|
238
226
|
@client.puts "PWD"
|
239
|
-
@client.gets.should == "257 \"
|
240
|
-
end
|
241
|
-
|
242
|
-
it "returns default directory on PWD" do
|
243
|
-
@server.directory = '/tmp'
|
244
|
-
@client.puts "PWD"
|
245
|
-
@client.gets.should == "257 \"/tmp\" is current directory\r\n"
|
227
|
+
@client.gets.should == "257 \"/pub\" is current directory\r\n"
|
246
228
|
end
|
247
229
|
|
248
230
|
it "says OK to any CWD, CDUP, without doing anything" do
|
249
|
-
directory = @server.directory
|
250
231
|
@client.puts "CWD somewhere/else"
|
251
232
|
@client.gets.should == "250 OK!\r\n"
|
252
|
-
@server.directory.should == directory
|
253
233
|
@client.puts "CDUP"
|
254
234
|
@client.gets.should == "250 OK!\r\n"
|
255
|
-
@server.directory.should == directory
|
256
235
|
end
|
257
236
|
|
258
237
|
it "does not respond to MKD" do
|
@@ -302,6 +281,7 @@ describe FakeFtp::Server do
|
|
302
281
|
@data_client.close
|
303
282
|
@client.gets.should == "226 Did it!\r\n"
|
304
283
|
@server.files.should include('some_file')
|
284
|
+
@server.file('some_file').bytes.should == 10
|
305
285
|
end
|
306
286
|
end
|
307
287
|
end
|
@@ -334,11 +314,15 @@ describe FakeFtp::Server do
|
|
334
314
|
@ftp.close
|
335
315
|
end
|
336
316
|
|
337
|
-
it "should put files
|
317
|
+
it "should put files using PASV" do
|
318
|
+
File.stat(@text_filename).size.should == 20
|
319
|
+
|
338
320
|
@ftp.connect('127.0.0.1', 21212)
|
339
321
|
@ftp.passive = true
|
340
322
|
proc { @ftp.put(@text_filename) }.should_not raise_error
|
323
|
+
|
341
324
|
@server.files.should include('text_file.txt')
|
325
|
+
@server.file('text_file.txt').bytes.should == 20
|
342
326
|
end
|
343
327
|
|
344
328
|
xit "should disconnect clients on close" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fake_ftp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Colin Shield
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-03-05 00:00:00 -08:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -83,10 +83,12 @@ files:
|
|
83
83
|
- Rakefile
|
84
84
|
- fake_ftp.gemspec
|
85
85
|
- lib/fake_ftp.rb
|
86
|
+
- lib/fake_ftp/file.rb
|
86
87
|
- lib/fake_ftp/server.rb
|
87
88
|
- lib/fake_ftp/version.rb
|
88
89
|
- spec/fixtures/invisible_bike.jpg
|
89
90
|
- spec/fixtures/text_file.txt
|
91
|
+
- spec/models/fake_ftp/file_spec.rb
|
90
92
|
- spec/models/fake_ftp/server_spec.rb
|
91
93
|
- spec/spec_helper.rb
|
92
94
|
has_rdoc: true
|
@@ -128,5 +130,6 @@ summary: Creates a fake FTP server for use in testing
|
|
128
130
|
test_files:
|
129
131
|
- spec/fixtures/invisible_bike.jpg
|
130
132
|
- spec/fixtures/text_file.txt
|
133
|
+
- spec/models/fake_ftp/file_spec.rb
|
131
134
|
- spec/models/fake_ftp/server_spec.rb
|
132
135
|
- spec/spec_helper.rb
|