{"id":19010286,"url":"https://github.com/railsware/capybara_mock","last_synced_at":"2025-04-22T23:13:12.469Z","repository":{"id":64677723,"uuid":"576391008","full_name":"railsware/capybara_mock","owner":"railsware","description":"CapybaraMock","archived":false,"fork":false,"pushed_at":"2025-04-22T19:49:28.000Z","size":56,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-22T23:13:04.149Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/railsware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-12-09T18:29:01.000Z","updated_at":"2025-04-22T19:49:30.000Z","dependencies_parsed_at":"2024-03-25T22:33:31.110Z","dependency_job_id":"d415813a-aea7-4bd8-993b-8b3eca1994e6","html_url":"https://github.com/railsware/capybara_mock","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.4285714285714286,"last_synced_commit":"b31412efcaf5f68e3a9983b183c5fee254a1e274"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/railsware%2Fcapybara_mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/railsware%2Fcapybara_mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/railsware%2Fcapybara_mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/railsware%2Fcapybara_mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/railsware","download_url":"https://codeload.github.com/railsware/capybara_mock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250337946,"owners_count":21414104,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T19:10:41.663Z","updated_at":"2025-04-22T23:13:12.452Z","avatar_url":"https://github.com/railsware.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![test](https://github.com/railsware/capybara_mock/actions/workflows/main.yml/badge.svg)](https://github.com/railsware/capybara_mock/actions/workflows/main.yml)\n\n# CapybaraMock\n\nLibrary for stubbing HTTP requests in Capybara browser. \n\n## Features\n\n* Stubbing interface similar to [WebMock](https://github.com/bblimke/webmock)\n* Matching requests based on\n  * method\n  * url\n  * query\n  * headers\n  * body\n\n## Supported capybara drivers\n\n* [Cuprite](https://github.com/rubycdp/cuprite)\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n    $ bundle add capybara_mock\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n\n    $ gem install capybara_mock\n\n### RSpec\n\nAdd the following code to `spec/spec_helper`:\n\n```ruby\nrequire 'capybara_mock/rspec'\n```\n\nThen ensure your capybara use `:cuprite` driver for feature specs that you want to mock browser requests.\n\n## Usage\n\n* `Capybara.stub_request` - stub external request by full url or regexp.\n* `Capybara.stub_path` - stub internal request (to your capybara application) by path.\n* `Capybara.remove_stub` - remove previously added stub to current capybara session\n* `Capybara.clear_stubs` - remove all stubs for current capybara session\n* `Capybara.save_unstubbed_requests` - save unstubbed browser requests from current capybara session. Useful for debugging.\n\n## Examples\n\n### Stub external request\n\n```ruby\nCapybaraMock.stub_request(\n  :get, 'https://api.stripe.com/v1/balance'\n).to_return(\n  status: 200,\n  headers: {'Content-Type' =\u003e 'application/json'},\n  body: {amount: 10100}.to_json\n)\n```\n\n### Stub internal request\n\n```ruby\nCapybaraMock.stub_path(\n  :get, '/api/users/me'\n).to_return(\n  status: 200,\n  headers: {'Content-Type' =\u003e 'application/json'},\n  body: {name: 'John Doe'}.to_json\n)\n```\n\n### Stub with specific query\n\n```ruby\nCapybaraMock.stub_path(\n  :get, '/api/users'\n).with(\n  query: {page: 1}\n).to_return(\n  status: 200,\n  headers: {'Content-Type' =\u003e 'application/json'},\n  body: [].to_json\n)\n```\n\n### Stub with specific header\n\n```ruby\nCapybaraMock.stub_path(\n  :get, '/api/users/1'\n).with(\n  headers: {\n    'Authorization' =\u003e 'Bearer ACCESS_TOKEN'\n  }\n).to_return(\n  status: 200,\n  headers: {'Content-Type' =\u003e 'application/json'},\n  body: {id: 1}.to_json\n)\n```\n\n### Stub with specific body\n\n```ruby\nCapybaraMock.stub_path(\n  :post, '/api/users'\n).with(\n  headers: {'Content-Type': 'application/x-www-form-urlencoded'}, \n  body: 'first_name=John\u0026last_name=Doe'\n).to_return(\n  status: 200,\n  headers: {'Content-Type' =\u003e 'application/json'},\n  body: {id: 1}.to_json\n)\n```\n\n```ruby\nCapybaraMock.stub_path(\n  :post, '/api/users'\n).with(\n  headers: {'Content-Type': 'application/json'}, \n  body: '{\"first_name\":\"John\",\"last_name\":\"Doe\"}'\n).to_return(\n  status: 200,\n  headers: {'Content-Type' =\u003e 'application/json'},\n  body: {id: 1}.to_json\n)\n```\n\n## Limitation\n\nUnfortunately Chrome DevTools Protocol still does not support all valid http response codes.\nRight now it [supports](https://github.com/chromium/chromium/blob/main/net/http/http_status_code_list.h) only:\n* 100, 101, 103\n* 200..206\n* 300..305, 307..308\n* 400..418, 425, 229\n* 500..505\n\nFor unsupported codes cuprite interceptor will send basic code and real code in special response header `X-Mock-Response-Status`. So you you have to add interceptor for your http client.\n\n\n### AXIOS interceptor\n\n```js\naxios.interceptors.response.use(\n  (response) =\u003e response,\n  (error) =\u003e {\n    if (error.response \u0026\u0026 error.response.headers['x-mock-response-status']) {\n      const status = parseInt(error.response.headers['x-mock-response-status'])\n      error.message = `Request failed with status code ${status}`\n      error.response.status = status\n    }\n\n   return Promise.reject(error)\n }\n```\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/railsware/capybara_mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/railsware/capybara_mock/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the CapybaraMock project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/railsware/capybara_mock/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frailsware%2Fcapybara_mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frailsware%2Fcapybara_mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frailsware%2Fcapybara_mock/lists"}