on 404 redirect for /owner/name paths on v1
This commit is contained in:
parent
a7d72beefc
commit
f8bd49e88b
lib/travis/api
spec
|
@ -16,7 +16,15 @@ require 'newrelic_rpm'
|
|||
# run Travis::Api::App.new
|
||||
#
|
||||
# Requires TLS in production.
|
||||
class Travis::Api::App
|
||||
module Travis::Api
|
||||
ACCEPT_VERSION = /vnd\.travis-ci\.(\d+)\+/
|
||||
DEFAULT_VERSION = 'v2'
|
||||
|
||||
def version(string)
|
||||
string =~ ACCEPT_VERSION && "v#{$1}" || DEFAULT_VERSION
|
||||
end
|
||||
|
||||
class App
|
||||
autoload :AccessToken, 'travis/api/app/access_token'
|
||||
autoload :Base, 'travis/api/app/base'
|
||||
autoload :Endpoint, 'travis/api/app/endpoint'
|
||||
|
@ -96,4 +104,5 @@ class Travis::Api::App
|
|||
def self.setup_endpoints
|
||||
Base.subclasses.each(&:setup)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
30
lib/travis/api/app/middleware/rewrite.rb
Normal file
30
lib/travis/api/app/middleware/rewrite.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
require 'travis/api/app'
|
||||
|
||||
class Travis::Api::App
|
||||
class Middleware
|
||||
class Rewrite < Middleware
|
||||
V1_REPO_URL = %r(^(/[^/]+/[^/]+(?:/builds(?:/[\d]+)?|/cc\.xml)?)$)
|
||||
|
||||
set(:setup) { ActiveRecord::Base.logger = Travis.logger }
|
||||
|
||||
after do
|
||||
p not_found?
|
||||
force_redirect("/repositories#{$1}") if response.status == 404 && version == 'v1' && request.path =~ V1_REPO_URL
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def force_redirect(path)
|
||||
response.body = ''
|
||||
response['Content-Length'] = '0'
|
||||
response['Content-Type'] = ''
|
||||
redirect(path)
|
||||
end
|
||||
|
||||
def version
|
||||
API.version(request.accept.join)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,8 +1,5 @@
|
|||
module Travis::Api::App::Responders
|
||||
class Json < Base
|
||||
ACCEPT_VERSION = /vnd\.travis-ci\.(\d+)\+/
|
||||
DEFAULT_VERSION = 'v2'
|
||||
|
||||
def apply?
|
||||
options[:format] == 'json' && !resource.is_a?(String)
|
||||
end
|
||||
|
@ -22,7 +19,7 @@ module Travis::Api::App::Responders
|
|||
end
|
||||
|
||||
def version
|
||||
request.accept.join =~ ACCEPT_VERSION && "v#{$1}" || DEFAULT_VERSION
|
||||
API.version(request.accept.join)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,14 +31,14 @@ describe 'Repos' do
|
|||
response.should deliver_json_for(Repository.by_slug('svenfuchs/minimal').first, version: 'v1')
|
||||
end
|
||||
|
||||
xit 'GET /svenfuchs/minimal' do
|
||||
it 'GET /svenfuchs/minimal' do
|
||||
response = get '/svenfuchs/minimal', {}, headers
|
||||
response.should deliver_json_for(Repository.by_slug('svenfuchs/minimal').first, version: 'v1')
|
||||
response.should redirect_to('/repositories/svenfuchs/minimal')
|
||||
end
|
||||
|
||||
xit 'GET /svenfuchs/minimal/cc.xml' do # TODO wat.
|
||||
it 'GET /svenfuchs/minimal/cc.xml' do
|
||||
response = get '/svenfuchs/minimal/cc.xml'
|
||||
response.should deliver_xml_for()
|
||||
response.should redirect_to('/repositories/svenfuchs/minimal/cc.xml')
|
||||
end
|
||||
|
||||
describe 'GET /svenfuchs/minimal.png' do
|
||||
|
|
|
@ -10,44 +10,7 @@ require 'multi_json'
|
|||
require 'travis/api/app'
|
||||
require 'travis/testing/scenario'
|
||||
require 'travis/testing/factories'
|
||||
|
||||
RSpec::Matchers.define :deliver_json_for do |resource, options = {}|
|
||||
match do |response|
|
||||
actual = parse(response.body)
|
||||
expected = Travis::Api.data(resource, options)
|
||||
|
||||
failure_message_for_should do
|
||||
"expected\n\n#{actual}\n\nto equal\n\n#{expected}"
|
||||
end
|
||||
|
||||
actual == expected
|
||||
end
|
||||
|
||||
def parse(body)
|
||||
MultiJson.decode(body)
|
||||
end
|
||||
end
|
||||
|
||||
RSpec::Matchers.define :deliver_result_image_for do |name|
|
||||
match do |response|
|
||||
actual = files.detect do |(name, content)|
|
||||
response.body.force_encoding('ascii') == content.force_encoding('ascii') # TODO ummmmmmmm?
|
||||
end
|
||||
actual = actual && actual[0]
|
||||
|
||||
failure_message_for_should do
|
||||
"expected #{actual.inspect} to equal #{name.inspect}"
|
||||
end
|
||||
|
||||
actual == name
|
||||
end
|
||||
|
||||
def files
|
||||
files = Hash[*Dir['public/images/result/*.png'].map do |file|
|
||||
[File.basename(file, '.png'), File.read(file)]
|
||||
end.flatten]
|
||||
end
|
||||
end
|
||||
require 'support/matchers'
|
||||
|
||||
Travis.logger = Logger.new(StringIO.new)
|
||||
Travis::Api::App.setup
|
||||
|
|
62
spec/support/matchers.rb
Normal file
62
spec/support/matchers.rb
Normal file
|
@ -0,0 +1,62 @@
|
|||
# TODO move to travis-core?
|
||||
|
||||
RSpec::Matchers.define :deliver_json_for do |resource, options = {}|
|
||||
match do |response|
|
||||
if response.status == 200
|
||||
actual = parse(response.body)
|
||||
expected = Travis::Api.data(resource, options)
|
||||
|
||||
failure_message_for_should do
|
||||
"expected\n\n#{actual}\n\nto equal\n\n#{expected}"
|
||||
end
|
||||
|
||||
actual == expected
|
||||
else
|
||||
failure_message_for_should do
|
||||
"expected the request to be successful (200) but was #{response.status}"
|
||||
end
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def parse(body)
|
||||
MultiJson.decode(body)
|
||||
end
|
||||
end
|
||||
|
||||
RSpec::Matchers.define :deliver_result_image_for do |name|
|
||||
match do |response|
|
||||
actual = files.detect do |(name, content)|
|
||||
response.body.force_encoding('ascii') == content.force_encoding('ascii') # TODO ummmmmmmm?
|
||||
end
|
||||
actual = actual && actual[0]
|
||||
|
||||
failure_message_for_should do
|
||||
"expected #{actual.inspect} to equal #{name.inspect}"
|
||||
end
|
||||
|
||||
actual == name
|
||||
end
|
||||
|
||||
def files
|
||||
files = Hash[*Dir['public/images/result/*.png'].map do |file|
|
||||
[File.basename(file, '.png'), File.read(file)]
|
||||
end.flatten]
|
||||
end
|
||||
end
|
||||
|
||||
RSpec::Matchers.define :redirect_to do |expected|
|
||||
match do |response|
|
||||
actual = response.headers['location'].to_s.sub('http://example.org', '')
|
||||
|
||||
failure_message_for_should do
|
||||
"expected to be redirect to #{expected} but was not. status: #{response.status}, location: #{actual}"
|
||||
end
|
||||
|
||||
failure_message_for_should_not do
|
||||
"expected not to be redirect to #{expected} but was."
|
||||
end
|
||||
|
||||
actual == expected
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user