-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
997b867
commit 630510e
Showing
15 changed files
with
271 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#TODO Modifications | ||
Conclusions | ||
Add "there are examples" somewhere towards the beginning...; | ||
When possible, it's great to test the server API via the client interface. (and still have separation of concerns though) | ||
|
||
#TODO use cistern in client example in sandbox mode | ||
|
||
Notes: | ||
|
||
In Sandbox mode, you could have a suite of fakewebs and run it twice like other modes, against the sandbox, HOWEVER: | ||
The scope of what fakeweb does is limited; It doesn't save the state of the server, and that's the essential difference. | ||
Meaning, you'll probably have tests that set things up in the server, and with a sandbox, tests depending on a certain state will work | ||
but with fakeweb you'll need to have a different type of response for each of those states, so in local development you'll still have to | ||
use a sandbox before using fakeweb. | ||
Also, wouldn't be simple to ship fixtures with the client with fakeweb, since it is very coupled to the implementation of the tests. | ||
|
||
https://github.com/lanej/cistern | ||
New undocumented project that is super cool and is great for the use-case of being stuck in sandboxing mode, still having mocks. | ||
It's not fully integrated: | ||
It can be fully integrated, if you run in two modes (like mentioned later with running two modes in a server) as such: | ||
one mode, you run the client specs against the real server (localhost, rack builder/capybara.app) and in a different mode | ||
run the client specs against the mocked server (bundled in the client, as mocks). | ||
|
||
It's a great way to add mocked mode to your api client even when you don't have access to server dev | ||
|
||
Even if it's not a full integration path (in later examples, running in both modes) | ||
In many times it's the only option you have, and it's still useful to have in third party apps using it. | ||
|
||
It's my birthday (rails conf) | ||
|
||
Examples | ||
Third party apps make real. # TODO | ||
Add in Faraday: done mocked mode. other modes explain # TODO? |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
gem 'rack-client' | ||
gem 'rack-test' | ||
gem 'fakeweb' | ||
gem 'pry' |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require './cool_musicians_app' | ||
require 'fakeweb' | ||
require 'json' | ||
require 'rack' | ||
require 'rack/client' | ||
require 'test/unit' | ||
require 'rack/test' | ||
|
||
class SocialMusicUploaderTest < Test::Unit::TestCase | ||
|
||
def setup | ||
@name = "funky" | ||
@words = "james brown is da bomb!" | ||
@muzik_label_client = MyMuzikLabelAPI::Client.new | ||
@muzik_label_client.domain = "mymuziklabel.localdev.com" | ||
# we need to mock out the server here, if we end up changing our api ../server/server this test will still pass | ||
# and our cool social app using our record labeling company won't have broken tests even though the upload is not working | ||
FakeWeb.register_uri(:post, "http://mymuziklabel.localdev.com/song/#{@name}", :body => {:status => "ok"}.to_json) | ||
FakeWeb.register_uri(:get, "http://mymuziklabel.localdev.com/song/#{@name}", :body => {:words => @words}.to_json ) | ||
end | ||
|
||
def test_it_uploads_to_muzik_label_through_our_interface | ||
SocialMusicUploader.upload_song_to_muzik_label(@name, @words) | ||
assert_equal @muzik_label_client.get_song(@name), @words | ||
end | ||
|
||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
gem 'faraday' | ||
gem 'rack-test' | ||
gem 'sinatra' | ||
gem 'pry' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'sinatra' | ||
require 'rack' | ||
require 'faraday' | ||
require 'json' | ||
|
||
# This is a copy over from the mapper pattern example in mapper/ using rack client. This demonstrates using faraday instead of rack-client. | ||
# Here I demonstrate using faraday with a fake app, but obviously the same holds for using this against real app (MyMuzikLabel.app) as well. | ||
|
||
module MyMuzikLabelAPI | ||
|
||
class Client | ||
# this is the change using faraday, it's a faraday compatible way of redirecting to a rack app (which we use as our fake). | ||
def mock!(backend) | ||
@connection = Faraday.new(:url => 'http://mymuziklabel.com') do |faraday| | ||
faraday.adapter :rack, backend | ||
end | ||
end | ||
def post_song(name, words) | ||
JSON.parse(@connection.post("/song/#{name}", {:words => words}.to_json).body)['status'] | ||
end | ||
def get_song(name) | ||
JSON.parse(@connection.get("/song/#{name}").body)['words'] | ||
end | ||
end | ||
|
||
class Server < Sinatra::Base | ||
post '/song/:name' do |name| | ||
Server.mapper.post_song(name, JSON.parse(env['rack.input'].read)['words']).to_json | ||
end | ||
get '/song/:name' do |name| | ||
Server.mapper.get_song(name).to_json | ||
end | ||
class << self | ||
attr_accessor :mapper | ||
end | ||
end | ||
|
||
class FakeMapper | ||
Store ||= Hash.new | ||
def self.post_song(name, words) | ||
Store[name] = words | ||
{:status => "ok"} | ||
end | ||
def self.get_song(name) | ||
{:words => Store[name]} | ||
end | ||
end | ||
|
||
def self.fake_app | ||
app = MyMuzikLabelAPI::Server | ||
app.mapper = MyMuzikLabelAPI::FakeMapper | ||
app | ||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require './api.rb' | ||
require 'sinatra' | ||
require 'rack' | ||
|
||
|
||
class MyMuzikLabel # A representation of a rails app | ||
|
||
# models | ||
class User; def self.has_too_many_songs?; false; end; end | ||
# controllers | ||
class Application < Sinatra::Base | ||
get '/' do; 'main server application where we are going to mount the external api we created.'; end | ||
end | ||
|
||
|
||
|
||
# our mapper: we can access all our server logic from within here that we didn't have access to in the api | ||
module MuzikMapper | ||
def self.post_song(name, words) | ||
unless User.has_too_many_songs? | ||
{:status => "ok"} # Song.create(:words => words, :name => name) | ||
end | ||
end | ||
def self.get_song(name) | ||
{:words => 'and all that jazz'} # {:words => Song.where(:name => name).words } | ||
end | ||
end | ||
|
||
|
||
|
||
# compare this to MyMuzikLabelAPI.fake_app which just uses the fake mapper instead. | ||
def self.app | ||
app = Rack::Cascade.new([MyMuzikLabelAPI::Server, MyMuzikLabel::Application]) | ||
MyMuzikLabelAPI::Server.mapper = MyMuzikLabel::MuzikMapper | ||
app | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require './api' | ||
require 'test/unit' | ||
|
||
# example testing the fake. still would need to test against the real. | ||
class ClientTestFake < Test::Unit::TestCase | ||
|
||
def setup | ||
@name = "rock" | ||
@words = "and all that jazz" | ||
@client = MyMuzikLabelAPI::Client.new | ||
@server = MyMuzikLabelAPI.fake_app | ||
@client.mock!(@server) | ||
end | ||
|
||
def test_it_can_store_and_retreives_a_song | ||
assert_equal @client.post_song("rock", "and all that jazz"), 'ok' | ||
assert_equal @client.get_song("rock"), "and all that jazz" | ||
end | ||
|
||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source 'https://rubygems.org' | ||
gem 'rack' | ||
gem 'rack-client' | ||
gem 'rack-test' | ||
gem 'fakeweb' | ||
gem 'pry' |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require './cool_musicians_app' | ||
require 'fakeweb' | ||
require 'json' | ||
require 'rack' | ||
require 'rack/client' | ||
require 'test/unit' | ||
require 'rack/test' | ||
|
||
class SocialMusicUploaderTest < Test::Unit::TestCase | ||
|
||
def setup | ||
@name = "funky" | ||
@words = "james brown is da bomb!" | ||
@muzik_label_client = MyMuzikLabelAPI::Client.new | ||
@muzik_label_client.domain = "mymuziklabel.localdev.com" | ||
|
||
# we need to mock out the server here, if we end up changing our api ../server/server this test will still pass | ||
# and our cool social app using our record labeling company won't have broken tests even though the upload is not working | ||
FakeWeb.register_uri(:post, "http://mymuziklabel.localdev.com/song/#{@name}", :body => {:status => "ok"}.to_json) | ||
FakeWeb.register_uri(:get, "http://mymuziklabel.localdev.com/song/#{@name}", :body => {:words => @words}.to_json ) | ||
|
||
# maybe, we would have access to a sandbox that muzik label gave us, we could use that in development/tests, however it would have | ||
# a bunch of drawbacks that the 'sandbox mode' introduces. | ||
end | ||
|
||
def test_it_uploads_to_muzik_label_through_our_interface | ||
SocialMusicUploader.upload_song_to_muzik_label(@name, @words) | ||
assert_equal @muzik_label_client.get_song(@name), @words | ||
end | ||
|
||
end |