Skip to content
Draft
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
12 changes: 12 additions & 0 deletions app/controllers/bugs/http_codes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Bugs::HttpCodesController < ApplicationController
def show
@status_code = params[:status].to_i

unless (400..599).cover?(@status_code)
return render plain: "Unsupported status code", status: :bad_request
end

@status_label = Rack::Utils::HTTP_STATUS_CODES[@status_code] || "Unknown Status"
render status: @status_code
end
end
17 changes: 17 additions & 0 deletions app/controllers/http_codes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class HttpCodesController < ApplicationController
CLIENT_ERROR_CODES = [ 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 418, 421, 422, 428, 429 ].freeze
SERVER_ERROR_CODES = [ 500, 501, 502, 503, 504, 505, 520 ].freeze

def index
@client_error_codes = build_error_codes(CLIENT_ERROR_CODES, "Client Error")
@server_error_codes = build_error_codes(SERVER_ERROR_CODES, "Server Error")
end

private

def build_error_codes(codes, fallback_label)
codes.map do |code|
{ code:, label: Rack::Utils::HTTP_STATUS_CODES[code] || fallback_label }
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class SessionsController < ApplicationController
def new
end

def create
cookies.encrypted.permanent[:authenticated] = true
redirect_to protected_path
Expand Down
7 changes: 7 additions & 0 deletions app/views/bugs/http_codes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% content_for :title, "HTTP #{@status_code}" %>

<div class="top-level-container">
<h1 class="hide@native margin-bs-xl text-large-title">HTTP <%= @status_code %></h1>
<p><strong>Status:</strong> <%= @status_code %> <%= @status_label %></p>
<p>This page intentionally responds with HTTP <%= @status_code %> for iOS error handling tests.</p>
</div>
28 changes: 28 additions & 0 deletions app/views/http_codes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<% content_for :title, "HTTP Codes" %>

<div class="top-level-container">
<h1 class="hide@native margin-bs-xl text-large-title">HTTP Codes</h1>
<p>Tap a status code below to trigger a response with that exact HTTP code.</p>

<h2 class="margin-bs-xl">Client errors (4xx)</h2>
<div class="formatted-list formatted-list--top-level">
<% @client_error_codes.each do |entry| %>
<%= render "navigations/item",
path: bugs_http_code_path(status: entry[:code]),
icon: "warning-bold",
name: entry[:code],
description: entry[:label] %>
<% end %>
</div>

<h2 class="margin-bs-xl">Server errors (5xx)</h2>
<div class="formatted-list formatted-list--top-level">
<% @server_error_codes.each do |entry| %>
<%= render "navigations/item",
path: bugs_http_code_path(status: entry[:code]),
icon: "warning-bold",
name: entry[:code],
description: entry[:label] %>
<% end %>
</div>
</div>
12 changes: 12 additions & 0 deletions app/views/resources/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
name: "Book",
description: "Hotwire Native for Rails Developers by Joe Masilotti." %>

<%= render "navigations/item",
path: native_docs_redirect_path,
icon: "arrow-square-out-bold",
name: "External redirect",
description: "Server-side redirect to native.hotwired.dev." %>

<%= render "navigations/item",
path: http_codes_path,
icon: "warning-bold",
name: "HTTP codes",
description: "Test HTTP error responses in the demo app." %>

<%= render "navigations/item",
path: long_resources_path,
icon: "scroll-bold",
Expand Down
12 changes: 8 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Rails.application.routes.draw do
resources :bugs, only: :index do
collection do
# get :some
end
resources :bugs, only: :index

namespace :bugs do
resources :http_codes, only: :show, path: "http", param: :status
end

resources :http_codes, only: :index

resources :components, only: %i[index new create] do
collection do
get :menu
Expand Down Expand Up @@ -48,12 +50,14 @@
get "/resource", to: "resources#show", as: :resource

resource :session, only: %i[new create destroy]
get "/signin", to: "sessions#new"
get "/protected", to: "sessions#protected"

direct(:docs) { "https://native.hotwired.dev" }
direct(:book) { "https://pragprog.com/titles/jmnative/hotwire-native-for-rails-developers/" }
direct(:bridge_components) { "https://native.hotwired.dev/overview/bridge-components" }

get :native_docs_redirect, to: redirect("https://native.hotwired.dev")
get :external_redirect, to: redirect("https://37signals.com")

# Defines the root path route ("/")
Expand Down
Loading