Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests and support for RSpec #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/remote_http_testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def json_response
@json_response ||= JSON.parse(last_response.body)
end

def response; last_response; end

# Temporarily make requests to a different server than the one specified by your server() method.
def use_server(server, &block)
@temporary_server = server
Expand Down
2 changes: 2 additions & 0 deletions remote_http_testing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_development_dependency 'rspec'
s.add_development_dependency 'fakeweb'
s.add_runtime_dependency "nokogiri"
s.add_runtime_dependency "json"
end
147 changes: 147 additions & 0 deletions spec/fakeweb/example.com.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
HTTP/1.1 200 OK
Server: nginx/1.0.13
Date: Fri, 13 Jul 2012 03:05:45 GMT
Content-Type: text/html
Content-Length: 3912
Last-Modified: Sat, 21 Mar 2009 19:30:36 GMT
Connection: keep-alive
Expires: Sat, 14 Jul 2012 03:05:45 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/jquery-ui.min.js" type="text/javascript"></script>
<!-- <script src="jquery-ui.js" type="text/javascript"></script> -->
<script type="text/javascript" language="javascript">
$(function(){
// set up keyboard
var topRow = $('<ul class="row" id="top">')
var homeRow = $('<ul class="row" id="home">')
var bottomRow = $('<ul class="row" id="bottom">')

$.each(['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{[', ']}'], function(_,key){
topRow.append(
$('<li />')
.attr('class', 'key')
.attr('id', key)
.html(key)
)
})
$.each(['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':;', '"\''], function(_,key){
homeRow.append(
$('<li />')
.attr('class', 'key')
.attr('id', key)
.html(key)
)
})
$.each(['Z', 'X', 'C', 'V', 'B', 'N', 'M', '<,', '>.', '/?'], function(_,key){
bottomRow.append(
$('<li />')
.attr('class', 'key')
.attr('id', key)
.html(key)
)
})
$('#keyboard')
.append(topRow)
.append(homeRow)
.append(bottomRow)

$('.key').draggable({
opacity: 0.35,
zIndex: 100,
snap: true,
snapMode: 'outer',
reset: true
})
$('.key').droppable({
drop: function(_,draggable){
var drag = $(draggable.draggable[0])
var drop = $(this)
var hold = $('<div>').insertAfter(drag)
drop.after(drag)
hold.after(drop).remove()
// clear the weird positioning
drag.css({top: '0px', left: '0px', position: 'relative'})
}
})

})
</script>
<style>
body {
font-size: 2em;
}
ul, li {
margin: 0;
padding: 0;
}
#container {
clear: both;
width: 24em;
margin: 2em auto;
text-align:; center;
}
#keyboard .row {
display: block;
float: left;
clear: both;
}
#keyboard .key {
display: block;
float: left;
height: 1.5em;
width: 2em;
padding-top: 0.3em;
text-align: center;
border-width: 2px;
border-style: ridged;
}
#keyboard .ui-draggable-dragging {
background-color: #EEE;
}
/* adjust alignment key row alignment */
#home {
margin-left: 0.89em;
}
#bottom {
margin-left: 1.8em;
}
#text {
float: left;
clear: both;
width: 100%;
margin-top: 2em;
padding: 2em 0;
text-align: center;
font-size: 0.5em;
border-top: 1px solid #AAA;
border-bottom: 1px solid #AAA;
}
</style>
</head>
<body>
<div id="container">
<div id="keyboard">
</div>
<div id="text">
You can touch type. Rearrange your keyboard to spell actual words.
<small>
<a href="http://www.youtube.com/watch?v=JjRTQIfVTgU">help</a>
</small>
</div>
</div>
</body>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-331450-11");
pageTracker._trackPageview();
} catch(err) {}</script>
</html>
80 changes: 80 additions & 0 deletions spec/fakeweb/example.com.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
HTTP/1.1 200 OK
X-RateLimit-Limit: 150
X-RateLimit-Remaining: 139
X-RateLimit-Reset: 1342150505
X-RateLimit-Class: api
Content-Type: application/json;charset=utf-8
Last-Modified: Fri, 13 Jul 2012 03:11:55 GMT
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Pragma: no-cache
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Set-Cookie: guest_id="v1:134214911540811807";Expires=Sun, 13-Jul-14 03:11:55 GMT;Path=/;Domain=.twitter.com
Status: 200 OK
X-Transaction: 8b09775fa3650061
X-Frame-Options: SAMEORIGIN
Date: Fri, 13 Jul 2012 03:11:55 GMT
Server: tfe
Content-Length: 29427

[
{
"created_at": "Wed Jul 11 05:57:28 +0000 2012",
"id": 222932599931285505,
"id_str": "222932599931285505",
"text": "@mrcs excellent work",
"source": "<a href=\"http://twitter.com/#!/download/ipad\" rel=\"nofollow\">Twitter for iPad</a>",
"truncated": false,
"in_reply_to_status_id": 222865614677622787,
"in_reply_to_status_id_str": "222865614677622787",
"in_reply_to_user_id": 101935227,
"in_reply_to_user_id_str": "101935227",
"in_reply_to_screen_name": "mrcs",
"user": {
"id": 12,
"id_str": "12",
"name": "Jack Dorsey",
"screen_name": "jack",
"location": "San Francisco",
"description": "Executive Chairman of Twitter, CEO of Square, a founder of both.",
"url": null,
"protected": false,
"followers_count": 2048806,
"friends_count": 1197,
"listed_count": 18245,
"created_at": "Tue Mar 21 20:50:14 +0000 2006",
"favourites_count": 1041,
"utc_offset": -28800,
"time_zone": "Pacific Time (US & Canada)",
"geo_enabled": true,
"verified": true,
"statuses_count": 11327,
"lang": "en",
"contributors_enabled": true,
"is_translator": false,
"profile_background_color": "EBEBEB",
"profile_background_image_url": "http://a0.twimg.com/images/themes/theme7/bg.gif",
"profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme7/bg.gif",
"profile_background_tile": false,
"profile_image_url": "http://a0.twimg.com/profile_images/1563216547/image_normal.jpg",
"profile_image_url_https": "https://si0.twimg.com/profile_images/1563216547/image_normal.jpg",
"profile_link_color": "990000",
"profile_sidebar_border_color": "DFDFDF",
"profile_sidebar_fill_color": "F3F3F3",
"profile_text_color": "333333",
"profile_use_background_image": true,
"show_all_inline_media": true,
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 2,
"favorited": false,
"retweeted": false
}
]
81 changes: 81 additions & 0 deletions spec/remote_testing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'spec_helper'

def tester
@tester ||= Class.new do
include RemoteHttpTesting
def server
DEFAULT_SERVER
end
def exit(*args); end
end.new
end

DEFAULT_SERVER = 'http://example.com'

describe RemoteHttpTesting do

include RemoteHttpTesting
let(:server) { DEFAULT_SERVER }

describe ".ensure_reachable!" do

before {
tester.should_receive(:server_reachable?).
with('http://example.com').
and_return(reachable?)
}
subject { tester.ensure_reachable! tester.server }

context "when reachable" do
let(:reachable?) { true }
it "silently continues" do
tester.should_not_receive(:exit)
tester.should_not_receive(:puts)
subject
end
end
context "when unreachable" do
let(:reachable?) { false }
it "bails" do
tester.should_receive(:puts)
tester.should_receive(:exit)
subject
end
end
end

describe '#current_server' do
subject { tester.current_server }
context "with a temporary server set" do
it "uses temporary server" do
tester.use_server('http://yahoo.com') do
tester.current_server.should == 'http://yahoo.com'
end
end
end
context "with no temporary server" do
tester.current_server.should == DEFAULT_SERVER
end
end

describe '#dom_response' do
before { get '/index.html' }
it "has DOM elements" do
dom_response.css('#container').children.size.should > 2
end
end

describe '#json_response' do
before { get '/index.json' }
it "is JSON data" do
json_response[0]["id"].should == 222932599931285505
end
end

describe '#response' do
before { get '/index.html' }
it "is the last HTTP response" do
response.should be_a(Net::HTTPOK)
end
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require File.expand_path('../../lib/remote_http_testing', __FILE__)
require 'fakeweb'

FakeWeb.register_uri :get,
'http://example.com/index.html',
:response => File.read('spec/fakeweb/example.com.html')
FakeWeb.register_uri :get,
'http://example.com/index.json',
:response => File.read('spec/fakeweb/example.com.json')