aboutsummaryrefslogtreecommitdiffstats
path: root/test/rubygems/test_gem_server.rb
blob: c283e661dc51570b178ca12d009cdcff50bed1a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'test/unit'
require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
require 'rubygems/server'
require 'stringio'

class Gem::Server
  attr_accessor :source_index
  attr_reader :server
end

class TestGemServer < RubyGemTestCase

  def setup
    super

    @a1 = quick_gem 'a', '1'

    @server = Gem::Server.new Gem.dir, 8809, false
    @req = WEBrick::HTTPRequest.new :Logger => nil
    @res = WEBrick::HTTPResponse.new :HTTPVersion => '1.0'
  end

  def test_quick_index
    data = StringIO.new "GET /quick/index HTTP/1.0\r\n\r\n"
    @req.parse data

    @server.quick @req, @res

    assert_equal 200, @res.status, @res.body
    assert_match %r| \d\d:\d\d:\d\d |, @res['date']
    assert_equal 'text/plain', @res['content-type']
    assert_equal "a-1", @res.body
  end

  def test_quick_index_rz
    data = StringIO.new "GET /quick/index.rz HTTP/1.0\r\n\r\n"
    @req.parse data

    @server.quick @req, @res

    assert_equal 200, @res.status, @res.body
    assert_match %r| \d\d:\d\d:\d\d |, @res['date']
    assert_equal 'text/plain', @res['content-type']
    assert_equal "a-1", Zlib::Inflate.inflate(@res.body)
  end

  def test_quick_a_1_gemspec_rz
    data = StringIO.new "GET /quick/a-1.gemspec.rz HTTP/1.0\r\n\r\n"
    @req.parse data

    @server.quick @req, @res

    assert_equal 200, @res.status, @res.body
    assert @res['date']
    assert_equal 'text/plain', @res['content-type']
    yaml = Zlib::Inflate.inflate(@res.body)
    assert_match %r|Gem::Specification|, yaml
    assert_match %r|name: a|, yaml
    assert_match %r|version: "1"|, yaml
  end

  def test_quick_a_1_mswin32_gemspec_rz
    a1_p = quick_gem 'a', '1' do |s| s.platform = Gem::Platform.local end
    si = Gem::SourceIndex.new @a1.full_name => @a1, a1_p.full_name => a1_p
    @server.source_index = si

    data = StringIO.new "GET /quick/a-1-#{Gem::Platform.local}.gemspec.rz HTTP/1.0\r\n\r\n"
    @req.parse data

    @server.quick @req, @res

    assert_equal 200, @res.status, @res.body
    assert @res['date']
    assert_equal 'text/plain', @res['content-type']
    yaml = Zlib::Inflate.inflate(@res.body)
    assert_match %r|Gem::Specification|, yaml
    assert_match %r|name: a|, yaml
    assert_match %r|version: "1"|, yaml
  end

  def test_quick_common_substrings
    ab1 = quick_gem 'ab', '1'
    si = Gem::SourceIndex.new @a1.full_name => @a1, ab1.full_name => ab1
    @server.source_index = si

    data = StringIO.new "GET /quick/a-1.gemspec.rz HTTP/1.0\r\n\r\n"
    @req.parse data

    @server.quick @req, @res

    assert_equal 200, @res.status, @res.body
    assert @res['date']
    assert_equal 'text/plain', @res['content-type']
    yaml = Zlib::Inflate.inflate @res.body
    assert_match %r|Gem::Specification|, yaml
    assert_match %r|name: a$|, yaml
    assert_match %r|version: "1"|, yaml
  end

  def test_quick_z_9_gemspec_rz
    data = StringIO.new "GET /quick/z-9.gemspec.rz HTTP/1.0\r\n\r\n"
    @req.parse data

    @server.quick @req, @res

    assert_equal 404, @res.status, @res.body
    assert_match %r| \d\d:\d\d:\d\d |, @res['date']
    assert_equal 'text/plain', @res['content-type']
    assert_equal 'No gems found matching "z" "9" nil', @res.body
    assert_equal 404, @res.status
  end

end