Class: HttpDecoy::RequestLog

Inherits:
Object
  • Object
show all
Defined in:
lib/http_decoy/request_log.rb

Overview

Thread-safe store of every request received by the fake server. Cleared between examples via Server#stop → new Server per example.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeRequestLog

Returns a new instance of RequestLog.



10
11
12
13
# File 'lib/http_decoy/request_log.rb', line 10

def initialize
  @entries = []
  @mutex   = Mutex.new
end

Instance Method Details

#allObject



27
28
29
# File 'lib/http_decoy/request_log.rb', line 27

def all
  @mutex.synchronize { @entries.dup }
end

#clearObject



36
37
38
# File 'lib/http_decoy/request_log.rb', line 36

def clear
  @mutex.synchronize { @entries.clear }
end

#countObject



40
41
42
# File 'lib/http_decoy/request_log.rb', line 40

def count
  @mutex.synchronize { @entries.size }
end

#for(method, path) ⇒ Object

Returns all entries matching the given HTTP method and exact path.



32
33
34
# File 'lib/http_decoy/request_log.rb', line 32

def for(method, path)
  all.select { |e| e.http_method == method.to_s.upcase && e.path == path }
end

#record(method:, path:, body:, headers:, query_params:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/http_decoy/request_log.rb', line 15

def record(method:, path:, body:, headers:, query_params:)
  @mutex.synchronize do
    @entries << Entry.new(
      http_method: method.to_s.upcase,
      path: path,
      body: body,
      headers: headers,
      query_params: query_params
    )
  end
end