Class: Shark::RSpec::FakeAssetService::Request

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shark/rspec/fake_asset_service/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setupObject



11
12
13
14
# File 'lib/shark/rspec/fake_asset_service/request.rb', line 11

def self.setup
  instance = self.instance
  instance.stub_requests
end

Instance Method Details

#hostObject



16
17
18
# File 'lib/shark/rspec/fake_asset_service/request.rb', line 16

def host
  Shark.configuration.asset_service.site
end

#log_info(message) ⇒ Object



121
122
123
# File 'lib/shark/rspec/fake_asset_service/request.rb', line 121

def log_info(message)
  Shark.logger.info message
end

#stub_requestsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/shark/rspec/fake_asset_service/request.rb', line 20

def stub_requests
  WebMock.stub_request(:get, uri_patterns[:resource]).to_return do |request|
    log_info '[Shark][AssetService] Faking GET request'

    id = extract_id_from_request_uri(request.uri)

    object_data = ObjectCache.instance.find(id)

    if object_data.present?
      SharkSpec.fake_response(200, data: object_data)
    else
      SharkSpec.fake_response(404, errors: [])
    end
  end

  WebMock.stub_request(:get, uri_patterns[:download]).to_return do |request|
    log_info '[Shark][AssetService] Faking GET request'

    public_id = extract_id_from_request_uri(request.uri)
    id = PublicId.decode_public_id(public_id)

    blob = ObjectCache.instance.find_blob(id)

    if blob.present?
      SharkSpec.fake_response(200, blob)
    else
      SharkSpec.fake_response(404, nil)
    end
  end

  WebMock.stub_request(:get, uri_patterns[:resources]).to_return do |_request|
    log_info '[Shark][AssetService] Faking GET request'
    SharkSpec.fake_response(200, data: ObjectCache.instance.objects)
  end

  WebMock.stub_request(:delete, uri_patterns[:resource]).to_return do |request|
    log_info '[Shark][AssetService] Faking DELETE request'

    id = extract_id_from_request_uri(request.uri)

    ObjectCache.instance.remove(id)

    SharkSpec.fake_response(204, nil)
  end

  WebMock.stub_request(:post, uri_patterns[:resources]).to_return do |request|
    log_info "[Shark][AssetService] Faking POST request with body: #{request.body}"

    payload = get_payload(request.body)
    object_data = ObjectCache.instance.add(payload)

    SharkSpec.fake_response(200, data: object_data)
  end

  WebMock.stub_request(:post, uri_patterns[:recreate_variations]).to_return do |request|
    log_info '[Shark][AssetService] Faking POST request'

    id = extract_id_from_request_uri(request.uri)

    if ObjectCache.instance.find(id)
      SharkSpec.fake_response(204, nil)
    else
      SharkSpec.fake_response(404, errors: [])
    end
  end

  WebMock.stub_request(:post, %r{^#{host}/packages}).to_return do |request|
    log_info "[Shark][AssetService] Faking POST request with body: #{request.body}"

    payload = get_payload(request.body)

    package_data = {
      attributes: {
        filename: payload['filename'],
        directory: payload['directory']
      }
    }

    object_data = ObjectCache.instance.add(package_data)

    SharkSpec.fake_response(200, data: object_data)
  end

  WebMock.stub_request(:get, %r{^#{host}/.+/download}).to_return do |_request|
    log_info '[Shark][AssetService] Faking GET download request'

    SharkSpec.fake_response(200, body: 'Lorem ipsum')
  end

  WebMock.stub_request(:put, %r{^#{host}/.+/upload}).to_return do |request|
    log_info '[Shark][AssetService] Faking PUT upload request'

    id = extract_id_from_request_uri(request.uri)
    object_data = ObjectCache.instance.find(id)

    object_data['attributes']['uploaded-at'] = 1 if object_data.present?

    SharkSpec.fake_response(200, body: '<response>true</response>')
  end
end