Created
December 2, 2009 23:40
-
-
Save dsturnbull/247733 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'uri' | |
| class Fetcher | |
| def fetch(url) | |
| parts = URI.parse(url) | |
| socket = TCPSocket.new(parts.host, parts.port) | |
| socket << "GET / HTTP/1.0\r\n" | |
| socket << "Host: #{parts.host}\r\n" | |
| socket << "\r\n" | |
| socket.read | |
| end | |
| def post(url, params) | |
| parts = URI.parse(url) | |
| socket = TCPSocket.new(parts.host, parts.port) | |
| socket << "POST / HTTP/1.0\r\n" | |
| socket << "Host: #{parts.host}\r\n" | |
| socket << "Content-Length: #{params.length}\r\n" | |
| socket << "#{params}\r\n" | |
| socket << "\r\n" | |
| end | |
| end | |
| describe Fetcher do | |
| before :all do | |
| @webpage = 'hello this is my page about my cat' | |
| @remote = mock('receiver') | |
| @remote.stub!(:<<).with(instance_of(String)) | |
| @remote.stub!(:read).and_return(@webpage) | |
| TCPSocket = mock('TCPSocket') | |
| TCPSocket.should_receive(:new).with( | |
| instance_of(String), instance_of(Fixnum) | |
| ).and_return(@remote) | |
| end | |
| it 'should fetch a webpage' do | |
| Fetcher.new.fetch('http://slashdot.org/').should == @webpage | |
| end | |
| it 'should post cat pictures' do | |
| Fetcher.new.post('http://slashdot.org/', 'a=1&b=2') | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment