Skip to content

Commit

Permalink
* write test codes by rspec.
Browse files Browse the repository at this point in the history
* follow error case problems.
  • Loading branch information
yagihiro committed Jan 1, 2009
1 parent 113ebf6 commit 00dcd31
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 17 deletions.
68 changes: 59 additions & 9 deletions lib/lwr-simple.rb
@@ -1,16 +1,45 @@
#
# = lwr-simple.rb
#
# Copyright(c) 2008 Hiroki Yagita
# LWR::Simple library is yet another LWP::Simple library for ruby.
#
# == What is this library?
# == SYNOPSIS:
#
# This library is the powerful web access library like LWP::Simple module.
# All methods are similar interface of LWP::Simple.
# It is an interface that assumes the scene to which LWR::Simple is taken an active part by the one liner like begin so LWP::Simple, too.
# Its interface really looks like the LWP::Simple.
#
# == Example
# # example. 1
# $ ruby -rlwr-simple -e 'getprint "http://www.sn.no"'
#
# nodoc...
# # example. 2
# $ ruby -rlwr-simple -e 'get("http://www.ruby-lang.org/ja/").scan(/<a.*href="(.+?)"/) {|refs| puts(refs[0])}'
#
# == FEATURES/PROBLEMS:
#
# See LWR::Simple module's documents.
#
# == LICENSE:
#
# Copyright (c) 2008 Hiroki Yagita
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# 'Software'), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

$:.unshift(File.dirname(__FILE__)) unless
Expand All @@ -30,9 +59,13 @@ module LWR # :nodoc:
module Simple
VERSION = '0.0.1'

module_function

# Fetch a resource by the given +url+, and return the resource as String.
def get url
_get(to_uri(url)).body
rescue
nil
end

# Get document headers - [+content_type+, +document_length+, +modified_time+, +expires+, +server+]
Expand All @@ -43,22 +76,35 @@ def head url

# +modified_time+ value is provisional it using +Date+ header.
[res.content_type, res.content_length, res["date"], res["expires"], res["server"]]
rescue
[]
end

# Fetch and print a resource by the given +url+.
def getprint url
uri = to_uri url
res = _get uri
puts res.body
res.code
res.code.to_i
rescue
code = 404
unless res
$stderr.puts "#{url} is bad URL."
else
code = res.code.to_i
$stderr.puts "#{url} is bad URL(#{code})."
end
code
end

# Fetch a resource by the given +url+ and save to +file+.
def getstore url, file
uri = to_uri url
res = _get uri
_store file, res.body
res.code
res.code.to_i
rescue
res ? res.code.to_i : 404
end

# Mirror a local file and a remote file method.
Expand All @@ -78,21 +124,25 @@ def mirror url, file
_store(file, _get(uri).body) unless code == 304

code
rescue
res ? res.code.to_i : 404
end

private
# only internal use.
def to_uri url
uri = URI.parse url
uri.path = "/" if uri.path.empty?
uri
end

# only internal use.
def _get uri
req = Net::HTTP::Get.new uri.path
res = Net::HTTP.start(uri.host, uri.port) {|http| http.request(req) }
res
end

# only internal use.
def _store path, s
File.open(path, "wb") {|f| f.write(s) }
end
Expand Down
103 changes: 95 additions & 8 deletions spec/lwr-simple_spec.rb
@@ -1,11 +1,98 @@
require File.dirname(__FILE__) + '/spec_helper.rb'

# Time to add your specs!
# http://rspec.info/
describe "Place your specs here" do

it "find this spec in spec directory" do
violated "Be sure to write your specs"
end

def successed? code
200 <= code and code < 400
end

# This spec quotes from the LWP::Simple documents.
describe LWR::Simple do

describe "get()" do
it "should be going to fetch the document identified by the given URL and return it." do
doc = LWR::Simple.get("http://www.google.com/")
doc.should_not be_nil
doc.should_not be_empty
doc.should be_an_instance_of(String)
doc.should match(/google/)
end
it "should nil if this method fails." do
LWR::Simple.get("http://www.notexists.com/").should be_nil
end
it "should URL argument can be either a String's instance." do
LWR::Simple.get(nil).should be_nil
LWR::Simple.get(URI("http://www.google.com/")).should be_nil
end
end

describe "head()" do
it "should get document headers. Returns the following 5 values if successful: ($content_type, $document_length, $modified_time, $expires, $server)" do
h = LWR::Simple.head("http://www.google.com/")
h.should_not be_nil
h.should_not be_empty
h.should be_an_instance_of(Array)
h.should have(5).items
end
it "should returns an empty list if this method fails." do
LWR::Simple.head("http://www.notexists.com/").should have(0).items
end
end

describe "getprint()" do
before do
@stdout = $>
@stderr = $stderr
end
after do
$> = @stdout
$stderr = @stderr
end
it "should get and print a document identified by a URL. The document is printed to the selected default filehandle for output (normally STDOUT) as data is received from the network." do
m = mock("out")
m.should_receive(:write) do |arg|
# +arg+ is printed a document as String instance.
arg.should be_an_instance_of(String)
arg.should_not be_empty
end
$> = m
code = LWR::Simple.getprint("http://www.google.com/")
successed?(code).should be_true
end
it "should if the request fails, then the status code and message are printed on STDERR. The return value is the HTTP response code." do
m = mock("err")
m.should_receive(:write).any_number_of_times # $stderr object requires #write method. (In fact, it is not called.)
m.should_receive(:puts) do |arg|
# +arg+ is printed a document as String instance.
arg.should be_an_instance_of(String)
arg.should_not be_empty
end
$stderr = m
LWR::Simple.getprint("http://www.notexists.com/").should == 404
end
end

describe "getstore()" do
after do
File.delete("g.html") if FileTest.exist?("g.html")
end
it "should gets a document identified by a URL and stores it in the file. The return value is the HTTP response code." do
code = LWR::Simple.getstore("http://www.google.com/", "g.html")
File.open("g.html") {|f| f.read.should match(/google/) }
successed?(code).should be_true
end
end

describe "mirror()" do
after do
File.delete("logo.gif") if FileTest.exist?("logo.gif")
end
it "should get and store a document identified by a URL, using If-modified-since, and checking the Content-Length. Returns the HTTP response code." do
code = LWR::Simple.mirror("http://www.ruby-lang.org/images/logo.gif", "logo.gif")
FileTest.exist?("logo.gif").should be_true
successed?(code).should be_true
code = LWR::Simple.mirror("http://www.ruby-lang.org/images/logo.gif", "logo.gif")
FileTest.exist?("logo.gif").should be_true
successed?(code).should be_true
end
end

end

0 comments on commit 00dcd31

Please sign in to comment.