diff options
author | Daigo 3 Dango <zunda@users.noreply.github.com> | 2017-06-05 10:09:29 +0000 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2017-06-05 12:09:29 +0200 |
commit | 2985d089517a4f83af1cff264d926955f74da2fd (patch) | |
tree | 8f07b8367dde89cb1441bd1774af8ce455a50330 | |
parent | 66ca7157db9c9ea24fd18a624a1a642c9f7a419a (diff) |
Redirect to streaming_api_base_url (#3579)
* Redirect to streaming_api_base_url When Rails receives a request to streaming API, it most likely means that there is another host which is configured to respond to it. This is to redirect clients to that host if `STREAMING_API_BASE_URL` is set as another host. * Use the new Ruby 1.9 hash syntax
-rw-r--r-- | app/controllers/api/v1/streaming_controller.rb | 15 | ||||
-rw-r--r-- | config/routes.rb | 1 | ||||
-rw-r--r-- | spec/controllers/api/v1/streaming_controller_spec.rb | 46 |
3 files changed, 62 insertions, 0 deletions
diff --git a/app/controllers/api/v1/streaming_controller.rb b/app/controllers/api/v1/streaming_controller.rb new file mode 100644 index 000000000..377951472 --- /dev/null +++ b/app/controllers/api/v1/streaming_controller.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class Api::V1::StreamingController < ApiController + respond_to :json + + def index + if Rails.configuration.x.streaming_api_base_url != request.host + uri = URI.parse(request.url) + uri.host = URI.parse(Rails.configuration.x.streaming_api_base_url).host + redirect_to uri.to_s, status: 301 + else + raise ActiveRecord::RecordNotFound + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 5c1c8e594..7a2286f92 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -141,6 +141,7 @@ Rails.application.routes.draw do resource :public, only: :show, controller: :public resources :tag, only: :show end + resources :streaming, only: [:index] get '/search', to: 'search#index', as: :search diff --git a/spec/controllers/api/v1/streaming_controller_spec.rb b/spec/controllers/api/v1/streaming_controller_spec.rb new file mode 100644 index 000000000..daf2807e7 --- /dev/null +++ b/spec/controllers/api/v1/streaming_controller_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::V1::StreamingController do + around(:each) do |example| + before = Rails.configuration.x.streaming_api_base_url + Rails.configuration.x.streaming_api_base_url = Rails.configuration.x.web_domain + example.run + Rails.configuration.x.streaming_api_base_url = before + end + + before(:each) do + request.headers.merge! Host: Rails.configuration.x.web_domain + end + + context 'with streaming api on same host' do + describe 'GET #index' do + it 'raises ActiveRecord::RecordNotFound' do + get :index + expect(response).to have_http_status(404) + end + end + end + + context 'with streaming api on different host' do + before(:each) do + Rails.configuration.x.streaming_api_base_url = 'wss://streaming-' + Rails.configuration.x.web_domain + @streaming_host = URI.parse(Rails.configuration.x.streaming_api_base_url).host + end + + describe 'GET #index' do + it 'redirects to streaming host' do + get :index, params: {access_token: 'deadbeef', stream: 'public'} + expect(response).to have_http_status(301) + request_uri = URI.parse(request.url) + redirect_to_uri = URI.parse(response.location) + [:scheme, :path, :query, :fragment].each do |part| + expect(redirect_to_uri.send(part)).to eq(request_uri.send(part)), "redirect target #{part}" + end + expect(redirect_to_uri.host).to eq(@streaming_host), "redirect target host" + end + end + end + +end |