Last active
August 29, 2015 14:16
-
-
Save maxmanders/ac0d530fc77b7adcd86c 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
$ curl -XGET -H "Content-Type: application/json" -H "Accept: application/json" "http://api.example.dev:13000/v1/echo/wibble" | |
{"message":"wibble"}% |
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
$ rake test | |
Run options: --seed 12839 | |
# Running: | |
F. | |
Finished in 0.035461s, 56.4000 runs/s, 56.4000 assertions/s. | |
1) Failure: | |
API::V1::StatusControllerTest#test_echo_route [/.../test/controllers/api/v1/status_controller_test.rb:5]: | |
No route matches "/v1/echo/wibble" | |
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips |
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
Rails.application.routes.draw do | |
namespace :api, :path => "", :constraints => { :subdomain => "api" }, :defaults => { :format => :json } do | |
namespace :v1 do | |
resources :users | |
get '/echo/:message', to: 'status#echo' | |
end | |
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
$ rake routes 1 ↵ | |
Prefix Verb URI Pattern Controller#Action | |
api_v1_users GET /v1/users(.:format) api/v1/users#index {:format=>:json, :subdomain=>"api"} | |
POST /v1/users(.:format) api/v1/users#create {:format=>:json, :subdomain=>"api"} | |
new_api_v1_user GET /v1/users/new(.:format) api/v1/users#new {:format=>:json, :subdomain=>"api"} | |
edit_api_v1_user GET /v1/users/:id/edit(.:format) api/v1/users#edit {:format=>:json, :subdomain=>"api"} | |
api_v1_user GET /v1/users/:id(.:format) api/v1/users#show {:format=>:json, :subdomain=>"api"} | |
PATCH /v1/users/:id(.:format) api/v1/users#update {:format=>:json, :subdomain=>"api"} | |
PUT /v1/users/:id(.:format) api/v1/users#update {:format=>:json, :subdomain=>"api"} | |
DELETE /v1/users/:id(.:format) api/v1/users#destroy {:format=>:json, :subdomain=>"api"} | |
api_v1 GET /v1/echo/:message(.:format) api/v1/status#echo {:format=>:json, :subdomain=>"api"} |
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
class API::V1::StatusController < ApplicationController | |
def echo | |
respond_to do |format| | |
format.json { render json: { :message => params[:message] } } | |
end | |
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
require 'test_helper' | |
class API::V1::StatusControllerTest < ActionController::TestCase | |
test "echo route" do | |
assert_routing "http://api.example.dev/v1/echo/wibble", { | |
:format => :json, | |
:subdomain => "api", | |
:controller => "api/v1/status", | |
:action => "echo", | |
:message => "wibble" } | |
end | |
test "should echo message" do | |
get :echo, :message => 'wibble', :format => 'json' | |
assert_response :success | |
json = JSON.parse(@response.body) | |
assert_equal "wibble", json["message"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment