lib/endpoint_stub.rb in endpoint_stub-1.1.0 vs lib/endpoint_stub.rb in endpoint_stub-1.4.5
- old
+ new
@@ -13,18 +13,16 @@
# Enable endpoint stubbing.
# This will cause all HTTP requests to raise an error,
# as per WebMock, unless relating to an ActiveResource
# model.
def self.activate!
- return if Config.activated
WebMock.enable!
Config.activated = true
end
# Disable endpoint stubbing.
# This allows real HTTP requests again.
def self.deactivate!
- return unless Config.activated
WebMock.disable!
Config.activated = false
end
# Calls deactivate, clears all stubs, then re-activates.
@@ -32,35 +30,48 @@
deactivate!
Endpoint::Stub.clear!
activate!
end
+ # Default to being deactivated.
+ deactivate!
+
# Feel free to add to these, and they will be applied to every
# stubbed endpoint thereafter.
Config.default_responses = [
### Index ###
[:get, '.json', ->(request, params, stub) {
- { body: stub.records }
+ query = request.uri.query_values
+
+ if !query || query.empty?
+ { body: stub.records }
+ else
+ {
+ body: stub.records.select do |record|
+ query.all? { |field, value| record[field] == value }
+ end
+ }
+ end
}],
### Show ###
[:get, '/:id.json', ->(request, params, stub) {
{ body: stub.records[params[:id].to_i] }
}],
### Create ###
[:post, '.json', ->(request, params, stub) {
- stub.add_record(JSON.parse(request.body))
- { body: '',
+ record = stub.add_record(JSON.parse(request.body))
+ { body: record,
status: 201,
headers: { 'Location' => stub.location(stub.last_id) }
}
}],
### Update ###
[:put, '/:id.json', ->(request, params, stub) {
if stub.update_record(params[:id], JSON.parse(request.body))
- { body: '', status: 204}
+ { body: stub.records[params[:id].to_i], status: 204}
else
{ body: "Failed to find #{stub.model_name} with id #{params[:id]}",
status: 404 }
end
}],