Skip to content

Commit

Permalink
Add some test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
James Conroy-Finn committed Oct 26, 2011
1 parent 0386abf commit e9f60a3
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 29 deletions.
52 changes: 28 additions & 24 deletions lib/net/nntp.rb
@@ -1,4 +1,7 @@
require 'socket'

$:.push File.expand_path('../..', __FILE__)

require 'net/nntp/version'
require 'net/nntp/article'
require 'net/nntp/response'
Expand All @@ -10,66 +13,55 @@ class NNTP

include Socket::Constants

attr_accessor :socket
attr_reader :response

def initialize(server, port = 119)
@server = server
@port = port

reconnect
@socket = Socket.new(AF_INET, SOCK_STREAM, 0)
self.socket = Socket.new(AF_INET, SOCK_STREAM, 0)

begin
@socket.connect(Socket.pack_sockaddr_in(port, server))
socket.connect(Socket.pack_sockaddr_in(@port, @server))
rescue Errno::EAFNOSUPPORT
@socket = Socket.new(AF_INET6, SOCK_STREAM, 0)
self.socket = Socket.new(AF_INET6, SOCK_STREAM, 0)
retry
end

response = Net::NNTP::Response.parse(@socket.readline)
response = Net::NNTP::Response.parse(socket.readline)

if response.code == 400 || response.code == 502
raise ServiceUnavailableException.new(response_code.to_s)
end
end

def close
@socket.close
socket.close
end

def closed?
@socket.closed?
socket.closed?
end

def reconnect
@socket = Socket.new(AF_INET, SOCK_STREAM, 0)
self.socket = Socket.new(AF_INET, SOCK_STREAM, 0)

begin
@socket.connect(Socket.pack_sockaddr_in(@port, @server))
socket.connect(Socket.pack_sockaddr_in(@port, @server))
rescue Errno::EAFNOSUPPORT
@socket = Socket.new(AF_INET6, SOCK_STREAM, 0)
self.socket = Socket.new(AF_INET6, SOCK_STREAM, 0)
retry
end

response = Net::NNTP::Response.parse(@socket.readline)
response = Net::NNTP::Response.parse(socket.readline)

if response.code == 400 || response.code == 502
raise ServiceUnavailableException.new(response_code.to_s)
end
end

def read_multiline(limit = nil)
lines, buffer = 0, ""
while true
read = @socket.readline
lines += 1
break if lines == limit || read.strip == '.'
buffer += read
end

buffer
end

def self.start(server, port = 119)
obj = self.new(server, port)
if block_given?
Expand Down Expand Up @@ -136,9 +128,21 @@ def auth(username, password)

private

def read_multiline(limit = nil)
lines, buffer = 0, ""
while true
read = socket.readline
lines += 1
break if lines == limit || read.strip == '.'
buffer += read
end

buffer
end

def ask(message)
@socket.write("#{message}\r\n")
@response = Net::NNTP::Response.parse(@socket.readline)
socket.write("#{message}\r\n")
@response = Net::NNTP::Response.parse(socket.readline)
end
end
end
2 changes: 1 addition & 1 deletion lib/net/nntp/version.rb
@@ -1,5 +1,5 @@
module Net
class NTTP
class NNTP
VERSION = '0.0.1'
end
end
2 changes: 1 addition & 1 deletion net-nttp.gemspec
Expand Up @@ -4,7 +4,7 @@ require 'net/nntp/version'

Gem::Specification.new do |s|
s.name = 'net-nntp'
s.version = Net::NTTP::VERSION
s.version = Net::NNTP::VERSION
s.authors = ['James Conroy-Finn']
s.email = ['james@logi.cl']
s.homepage = 'https://github.com/jcf/net-nttp'
Expand Down
79 changes: 78 additions & 1 deletion spec/net/nntp_spec.rb
@@ -1,4 +1,81 @@
require 'spec_helper'

describe Net::NTTP do
describe Net::NNTP do
let(:socket) { double('socket').as_null_object }

let(:nntp) do
Net::NNTP.new('news.example.com')
end

before(:each) do
Socket.stub(pack_sockaddr_in: socket)
Socket.stub(new: socket)
end

describe '#close' do
it 'closes the socket' do
socket.should_receive(:close)
nntp.close
end
end

describe '#closed?' do
it 'closes the socket' do
socket.stub(closed?: true)
nntp.should be_closed
end
end

describe '#mode_reader' do
it 'asks for mode reader' do
socket.should_receive(:write).with("MODE READER\r\n")
nntp.mode_reader
end
end

describe '#listgroup' do
context 'with a group of comp.lang.ruby' do
it 'asks to list the comp.lang.ruby group' do
socket.should_receive(:write).with("LISTGROUP comp.lang.ruby\r\n")
nntp.listgroup('comp.lang.ruby')
end
end
end

describe '#article' do
context 'with ID 11' do
it 'asks for the article with ID 11' do
socket.should_receive(:write).with("ARTICLE 11\r\n")
nntp.article(11)
end
end
end

describe '#head' do
context 'with an ID of 11' do
it 'asks for head with ID 11' do
socket.should_receive(:write).with("HEAD 11\r\n")
nntp.head(11)
end
end

# context 'with a 221 response code' do
# it 'returns a parsed article' do
# Net::NNTP::Article.stub(parse: 'article')
# socket.stub(write: true)
# nntp.stub_chain(:response, :code).and_return(221)
# nntp.head(11).should == 'article'
# end
# end
end

describe '#group' do
context 'with a newsgroup of comp.lang.ruby' do
it 'asks for group with comp.lang.ruby' do
socket.should_receive(:write).with("GROUP comp.lang.ruby\r\n")
nntp.group('comp.lang.ruby')
end
end
end

end
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
@@ -1,4 +1,8 @@
require 'rspec'
require 'simplecov'
$:.push File.expand_path('../../lib', __FILE__)
require 'net/nntp'
require File.expand_path('../../lib/net/nntp', __FILE__)

Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }

RSpec.configure do |config|
end

0 comments on commit e9f60a3

Please sign in to comment.