Class: Async::HTTP::Mock::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/mock/endpoint.rb

Overview

This is an endpoint which bridges a client with a local server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol = Protocol::HTTP2, scheme = "http", authority = "localhost", queue: Queue.new) ⇒ Endpoint

Returns a new instance of Endpoint.



32
33
34
35
36
37
38
# File 'lib/async/http/mock/endpoint.rb', line 32

def initialize(protocol = Protocol::HTTP2, scheme = "http", authority = "localhost", queue: Queue.new)
	@protocol = protocol
	@scheme = scheme
	@authority = authority
	
	@queue = queue
end

Instance Attribute Details

#authorityObject (readonly)

Returns the value of attribute authority.



42
43
44
# File 'lib/async/http/mock/endpoint.rb', line 42

def authority
  @authority
end

#protocolObject (readonly)

Returns the value of attribute protocol.



40
41
42
# File 'lib/async/http/mock/endpoint.rb', line 40

def protocol
  @protocol
end

#schemeObject (readonly)

Returns the value of attribute scheme.



41
42
43
# File 'lib/async/http/mock/endpoint.rb', line 41

def scheme
  @scheme
end

Instance Method Details

#connectObject



56
57
58
59
60
61
62
# File 'lib/async/http/mock/endpoint.rb', line 56

def connect
	local, remote = ::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)
	
	@queue.enqueue(remote)
	
	return local
end

#run(parent: Task.current) {|::HTTP::Protocol::Request| ... } ⇒ Object

Processing incoming connections

Yields:

  • (::HTTP::Protocol::Request)

    the requests as they come in.



46
47
48
49
50
51
52
53
54
# File 'lib/async/http/mock/endpoint.rb', line 46

def run(parent: Task.current, &block)
	while peer = @queue.dequeue
		server = @protocol.server(peer)
		
		parent.async do
			server.each(&block)
		end
	end
end

#wrap(endpoint) ⇒ Object



64
65
66
# File 'lib/async/http/mock/endpoint.rb', line 64

def wrap(endpoint)
	self.class.new(@protocol, endpoint.scheme, endpoint.authority, queue: @queue)
end