|
| 1 | +require 'spec_helper' |
| 2 | +require 'puppet_forge_server' |
| 3 | + |
| 4 | +describe PuppetForgeServer::Http::HttpClient do |
| 5 | + let(:port) do |
| 6 | + require 'socket' |
| 7 | + server = TCPServer.new('127.0.0.1', 0) |
| 8 | + port = server.addr[1] |
| 9 | + server.close |
| 10 | + port |
| 11 | + end |
| 12 | + let(:uri) { "http://localhost:#{port}/" } |
| 13 | + before(:each) do |
| 14 | + @server = TCPServer.new('localhost', port) |
| 15 | + @thr = Thread.new do |
| 16 | + loop do |
| 17 | + socket = @server.accept |
| 18 | + response = "Hello!" |
| 19 | + socket.print "HTTP/1.1 200 OK\r\n" + |
| 20 | + "Content-Type: text/plain\r\n" + |
| 21 | + "Content-Length: #{response.bytesize}\r\n" + |
| 22 | + "Connection: close\r\n" |
| 23 | + socket.print "\r\n" |
| 24 | + sleep 0.01 |
| 25 | + socket.print response |
| 26 | + socket.close |
| 27 | + end |
| 28 | + end |
| 29 | + end |
| 30 | + after(:each) do |
| 31 | + @server.close |
| 32 | + @thr.kill |
| 33 | + end |
| 34 | + let(:instance) { described_class.new(cache) } |
| 35 | + describe '#download' do |
| 36 | + subject do |
| 37 | + 99.times { instance.download(uri) } |
| 38 | + instance.download(uri) |
| 39 | + end |
| 40 | + context 'with 1sec LRU cache' do |
| 41 | + let(:cache) do |
| 42 | + require 'lrucache' |
| 43 | + LRUCache.new(:ttl => 1) |
| 44 | + end |
| 45 | + it { expect(subject).not_to be_nil } |
| 46 | + it { expect(subject).not_to be_closed } |
| 47 | + end |
| 48 | + context 'with default cache' do |
| 49 | + let(:cache) { nil } |
| 50 | + it { expect(subject).not_to be_nil } |
| 51 | + it { expect(subject).not_to be_closed } |
| 52 | + end |
| 53 | + end |
| 54 | + describe '#get' do |
| 55 | + subject do |
| 56 | + 99.times { instance.get(uri) } |
| 57 | + instance.get(uri) |
| 58 | + end |
| 59 | + context 'with 1sec LRU cache' do |
| 60 | + let(:cache) do |
| 61 | + require 'lrucache' |
| 62 | + LRUCache.new(:ttl => 1) |
| 63 | + end |
| 64 | + it { expect(subject).to eq('Hello!') } |
| 65 | + end |
| 66 | + context 'with default cache' do |
| 67 | + let(:cache) { nil } |
| 68 | + it { expect(subject).to eq('Hello!') } |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments