Created
August 7, 2012 20:00
-
-
Save anandkunal/3288840 to your computer and use it in GitHub Desktop.
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
<? | |
include("toro.php"); | |
// Domain object, not included, just for following along | |
include("domain/user.php"); | |
class V1UserCreateHandler extends ToroHandler { | |
private function fail($error_type="") { | |
echo json_encode(array("success" => false, "error_type" => $error_type)); | |
exit; | |
} | |
public function post() { | |
if (isset($_POST['stash'])) { | |
// Fake call, assume this returns true/false | |
// Expecting a json string with id and array of ids (friends) | |
$user = User::create_user_from_json_string($_POST['stash']); | |
if ($user) { | |
echo json_encode(array("success" => true)); | |
exit; | |
} | |
else { | |
$this->fail("InvalidUser"); | |
} | |
} | |
else { | |
$this->fail("MissingStash"); | |
} | |
} | |
} | |
// Routes (we're only testing one route) | |
$api = new ToroApplication(Array( | |
Array('/api/v1/user/create', 'V1UserCreateHandler'), | |
)); | |
$api->serve(); | |
?> |
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
def dummy | |
"#{Time.now.to_i.to_s}#{Time.now.usec.to_s}" | |
end | |
def create_user(stash) | |
Nestful.post "#{@api}/user/create", :format => :json, :params => { :stash => stash } | |
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
require 'rubygems' | |
require 'nestful' | |
require 'test/unit' | |
class TestV1Endpoints < Test::Unit::TestCase | |
def setup | |
# Point to your local PHP instance | |
@root = "http://local.site.com" | |
@api = "#{@root}/api/v1/" | |
end | |
def test_domain_root_is_missing | |
assert_raise(Nestful::ResourceNotFound) { Nestful.get "#{@root}" } | |
end | |
def test_api_root_is_missing | |
assert_raise(Nestful::ResourceNotFound) { Nestful.get "#{@api}" } | |
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
def test_user_create_via_get_is_missing | |
assert_raise(Nestful::ResourceNotFound) { Nestful.get "#{@api}/user/create" } | |
end | |
def test_user_create_via_post_with_empty_stash_fails | |
response = Nestful.post "#{@api}/user/create", :format => :json | |
assert_equal(false, response["success"]) | |
assert_equal("MissingStash", response["error_type"]) | |
end | |
def test_user_create_via_post_with_missing_stash_vars_fails | |
stash = "{'player_id':'#{dummy}'}" | |
response = create_user(stash) | |
assert_equal(false, response["success"]) | |
assert_equal("InvalidUser", json["error_type"]) | |
end | |
def test_user_create_via_post_with_invalid_stash_types_fails | |
stash = "{'player_id':'#{dummy}', 'friend_ids':'not an array'}" | |
response = create_user(stash) | |
assert_equal(false, response["success"]) | |
assert_equal("InvalidUser", response["error_type"]) | |
end | |
def test_user_create_via_post_with_empty_friend_ids_passes | |
stash = "{'player_id':'#{dummy}', 'friend_ids':[]}" | |
response = create_user(stash) | |
assert_equal(true, response["success"]) | |
end | |
def test_user_create_via_post_with_valid_stash_passes | |
stash = "{'player_id':'#{dummy}', 'friend_ids':[#{dummy},#{dummy}]}" | |
response = create_user(stash) | |
assert_equal(true, response["success"]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment