oaf 0.4.0 → 0.4.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 720ac500bd3afa837043ed6adff3b3e57d9e75a9
4
- data.tar.gz: e3b3c50d97dd65860a2793ccb41db1ab80de1a79
3
+ metadata.gz: c8a01245a80af4f9a99bc7c2cb30a687ecf47225
4
+ data.tar.gz: a3facc728430cd4a3625ffb3a3b571739f7f6597
5
5
  SHA512:
6
- metadata.gz: b282c4c90f970cbdd35d0cde3d0491687e9b66f1536eb4d0bf296dc319d7a6c8343b06b2d3502c02ec76b9fa24784f20e28790dd2dec322f85be8b233f7da4d1
7
- data.tar.gz: d89e72758ca3113af24fe337ce8aa4e870ce5095fb52c3f71ba1048bd7450b21d008805412763e05bf86b365d3854c65627efc4c9d3171ecbe485d7e941e288c
6
+ metadata.gz: 973ea45b74b7078cc34b7977c158d04c366bad98a7d85e3eed22b6bba3adebd2ed95ea523ea45b01fe7d5716ceb69a39990c73495b242ce56ada7b0373760b7a
7
+ data.tar.gz: 415103896f41e9bfb869106bc176271833981876f354ef111c18afcaf4eff7ca64b1970882c8062bde57f2110d0d6a5720b86cb5e4704d6f047c01ea0881c6a0
data/bin/oaf CHANGED
@@ -3,7 +3,10 @@
3
3
  require 'optparse'
4
4
  require 'oaf'
5
5
 
6
- options = {:port => 9000}
6
+ options = {
7
+ :port => 9000,
8
+ :default_response => ''
9
+ }
7
10
 
8
11
  begin
9
12
  OptionParser.new do |opts|
@@ -16,6 +19,11 @@ begin
16
19
  exit 1
17
20
  end
18
21
  options[:port] = v.to_s
22
+ ARGV.shift
23
+ end
24
+ opts.on('--default-response FILE', 'Path to default response file') do |v|
25
+ options[:default_response] = v.to_s
26
+ ARGV.shift
19
27
  end
20
28
  opts.on('--version', 'Show the version number') do
21
29
  puts Oaf::VERSION
@@ -40,4 +48,4 @@ else
40
48
  exit 1
41
49
  end
42
50
 
43
- Oaf::HTTPServer.serve options[:path], options[:port]
51
+ Oaf::HTTPServer.serve options
@@ -22,12 +22,12 @@ module Oaf
22
22
  # == Parameters:
23
23
  # server::
24
24
  # A WEBrick::HTTPServer object
25
- # path::
26
- # A string containing the root path
25
+ # options::
26
+ # A hash of Oaf configuration options
27
27
  #
28
- def initialize server, path
28
+ def initialize server, options
29
29
  super server
30
- @path = path
30
+ @options = options
31
31
  end
32
32
 
33
33
  # Main server method. Oaf does not really differentiate between different
@@ -43,9 +43,10 @@ module Oaf
43
43
  req_headers = req.header
44
44
  req_query = req.query
45
45
  req_body = Oaf::HTTPServer.get_request_body req
46
- file = Oaf::Util.get_request_file @path, req.path, req.request_method
46
+ file = Oaf::Util.get_request_file(@options[:path], req.path,
47
+ req.request_method, @options[:default_response])
47
48
  out = Oaf::Util.get_output(file, req.path, req_headers, req_body,
48
- req_query)
49
+ req_query)
49
50
  res_headers, res_status, res_body = Oaf::HTTPServer.parse_response out
50
51
  Oaf::HTTPServer.set_response! res, res_headers, res_body, res_status
51
52
  end
@@ -70,14 +70,12 @@ module Oaf::HTTPServer
70
70
  # routes them to the appropriate scripts if they exist on the filesystem.
71
71
  #
72
72
  # == Parameters:
73
- # path::
74
- # The path in which to search for files
75
- # port::
76
- # The TCP port to listen on
73
+ # options::
74
+ # A hash of Oaf configuration options
77
75
  #
78
- def serve path, port
79
- server = WEBrick::HTTPServer.new :Port => port
80
- server.mount '/', Oaf::HTTPHandler, path
76
+ def serve options
77
+ server = WEBrick::HTTPServer.new :Port => options[:port]
78
+ server.mount '/', Oaf::HTTPHandler, options
81
79
  trap 'INT' do server.shutdown end
82
80
  server.start
83
81
  end
@@ -204,28 +204,25 @@ module Oaf::Util
204
204
  # The HTTP request path
205
205
  # method::
206
206
  # The HTTP method of the request
207
+ # default::
208
+ # An optional path to a default response file
207
209
  #
208
210
  # == Returns:
209
211
  # The path to a file to use, or `nil` if none is found.
210
212
  #
211
- def get_request_file root, req_path, method
213
+ def get_request_file root, req_path, method, default=''
212
214
  path = File.join root, req_path
213
215
  file = "#{path}.#{method}"
214
216
 
215
- if not File.exist? file
216
- if File.directory? path
217
- Dir.glob(File.join(path, "_*_.#{method}")).each do |f|
218
- file = f
219
- break
220
- end
221
- else
222
- Dir.glob(File.join(File.dirname(file), "_*_.#{method}")).each do |f|
223
- file = f
224
- break
225
- end
226
- end
217
+ return file if File.file? file
218
+
219
+ Dir.glob(File.join(path, "_*_.#{method}")).each do |f|
220
+ return f
221
+ end
222
+ Dir.glob(File.join(File.dirname(file), "_*_.#{method}")).each do |f|
223
+ return f
227
224
  end
228
- File.exist?(file) ? file : nil
225
+ File.file?(default.to_s) ? default : nil
229
226
  end
230
227
 
231
228
  # Fork a new process, in which we can safely modify the running environment
@@ -1,3 +1,3 @@
1
1
  module Oaf
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
@@ -81,19 +81,20 @@ module Oaf
81
81
  end
82
82
 
83
83
  it "should start an HTTP server" do
84
+ options = {:path => '/tmp', :port => 9000}
84
85
  @webrick = double()
85
86
  @webrick.should_receive(:start).once.and_return(true)
86
87
  WEBrick::HTTPServer.stub(:new).and_return(@webrick)
87
88
  @webrick.should_receive(:mount) \
88
- .with('/', Oaf::HTTPHandler, '/tmp').once \
89
+ .with('/', Oaf::HTTPHandler, options).once \
89
90
  .and_return(true)
90
- Oaf::HTTPServer.serve '/tmp', 9000
91
+ Oaf::HTTPServer.serve options
91
92
  end
92
93
 
93
94
  it "should parse the request properly" do
94
95
  req = Oaf::FakeReq.new :path => @f1request
95
96
  res = Oaf::FakeRes.new
96
- handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, @tempdir1
97
+ handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, {:path => @tempdir1}
97
98
  handler.process_request req, res
98
99
  res.body.should eq("This is a test.\n")
99
100
  res.status.should eq(201)
@@ -103,7 +104,7 @@ module Oaf
103
104
  it "should accept containable methods properly" do
104
105
  req = Oaf::FakeReq.new({:path => @f2request, :method => 'PUT'})
105
106
  res = Oaf::FakeRes.new
106
- handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, @tempdir1
107
+ handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, {:path => @tempdir1}
107
108
  handler.process_request req, res
108
109
  res.body.should eq("Containable Test\n")
109
110
  res.status.should eq(202)
@@ -114,7 +115,7 @@ module Oaf
114
115
  req = Oaf::FakeReq.new :path => @f1request
115
116
  res = Oaf::FakeRes.new
116
117
  Oaf::HTTPHandler.any_instance.stub(:process_request).and_return(true)
117
- handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, @tempdir1
118
+ handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, {:path => @tempdir1}
118
119
  handler.should_receive(:process_request).with(req, res).once
119
120
  handler.respond_to?(:do_GET).should be_true
120
121
  handler.respond_to?(:do_get).should be_false
@@ -126,7 +127,7 @@ module Oaf
126
127
  req = Oaf::FakeReq.new :path => @f1request
127
128
  res = Oaf::FakeRes.new
128
129
  Oaf::HTTPHandler.any_instance.stub(:process_request).and_return(true)
129
- handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, @tempdir1
130
+ handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, {:path => @tempdir1}
130
131
  handler.should_receive(:process_request).with(req, res).exactly(4).times
131
132
  handler.do_GET(req, res)
132
133
  handler.do_POST(req, res)
@@ -137,7 +138,7 @@ module Oaf
137
138
  it "should use directory default if no higher-level script exists" do
138
139
  req = Oaf::FakeReq.new :path => @f3request
139
140
  res = Oaf::FakeRes.new
140
- handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, @tempdir2
141
+ handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, {:path => @tempdir2}
141
142
  handler.process_request req, res
142
143
  res.body.should eq("Directory wins\n")
143
144
  end
@@ -145,7 +146,7 @@ module Oaf
145
146
  it "should use a file if present with a similarly-named directory" do
146
147
  req = Oaf::FakeReq.new :path => @f4request
147
148
  res = Oaf::FakeRes.new
148
- handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, @tempdir3
149
+ handler = Oaf::HTTPHandler.new Oaf::FakeServlet.new, {:path => @tempdir3}
149
150
  handler.process_request req, res
150
151
  res.body.should eq("File wins\n")
151
152
  end
@@ -144,6 +144,7 @@ module Oaf
144
144
  before(:all) do
145
145
  @tempdir1 = Dir.mktmpdir
146
146
  @tempdir2 = Dir.mktmpdir
147
+ @tempdir3 = Dir.mktmpdir
147
148
 
148
149
  @f1 = Tempfile.new ['oaf', '.GET'], @tempdir1
149
150
  @f1.write "This is a test.\n"
@@ -160,6 +161,7 @@ module Oaf
160
161
  @f2.delete
161
162
  Dir.delete @tempdir1
162
163
  Dir.delete @tempdir2
164
+ Dir.delete @tempdir3
163
165
  end
164
166
 
165
167
  it "should find existing files correctly" do
@@ -168,12 +170,22 @@ module Oaf
168
170
  end
169
171
 
170
172
  it "should return the fall-through file if request file doesn't exist" do
171
- result = Oaf::Util.get_request_file @tempdir1, 'nonexistent', 'GET'
173
+ result = Oaf::Util.get_request_file @tempdir1, 'na', 'GET'
172
174
  result.should eq(@f2.path)
173
175
  end
174
176
 
177
+ it "should return a custom default if configured" do
178
+ result = Oaf::Util.get_request_file @tempdir3, 'na', 'GET', @f1.path
179
+ result.should eq(@f1.path)
180
+ end
181
+
182
+ it "should still return the fall-through if default doesn't exist" do
183
+ result = Oaf::Util.get_request_file @tempdir3, 'na', 'GET', '/n0n3x1st3nt'
184
+ result.should eq(nil)
185
+ end
186
+
175
187
  it "should return nil if neither the requested or default file exist" do
176
- result = Oaf::Util.get_request_file @tempdir2, 'nonexistent', 'GET'
188
+ result = Oaf::Util.get_request_file @tempdir2, 'na', 'GET'
177
189
  result.should be_nil
178
190
  end
179
191
  end
@@ -256,8 +268,9 @@ module Oaf
256
268
  end
257
269
 
258
270
  it "should error if the passed path does not exist" do
259
- result = Oaf::Util.run_buffered '/nonexistent', {}, ''
260
- result.should eq("No such file or directory - /nonexistent")
271
+ # /nonexistent exists occasionally on Travis CI, so use a weird directory
272
+ result = Oaf::Util.run_buffered '/n0n3x1st3nt', {}, ''
273
+ result.should eq("No such file or directory - /n0n3x1st3nt")
261
274
  end
262
275
  end
263
276
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Uber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-20 00:00:00.000000000 Z
11
+ date: 2014-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake