{"id":19306550,"url":"https://github.com/sonicoder86/ngx-http-test","last_synced_at":"2025-04-22T12:36:03.597Z","repository":{"id":57179601,"uuid":"76980493","full_name":"sonicoder86/ngx-http-test","owner":"sonicoder86","description":"Angular 2 Http test helper","archived":false,"fork":false,"pushed_at":"2017-09-05T07:21:08.000Z","size":34,"stargazers_count":17,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-09-28T09:06:57.269Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sonicoder86.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-20T18:12:29.000Z","updated_at":"2020-08-16T16:31:26.000Z","dependencies_parsed_at":"2022-09-14T02:32:04.710Z","dependency_job_id":null,"html_url":"https://github.com/sonicoder86/ngx-http-test","commit_stats":null,"previous_names":["blacksonic/angular2-http-testing","blacksonic/ngx-http-test","sonicoder86/ngx-http-test"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonicoder86%2Fngx-http-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonicoder86%2Fngx-http-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonicoder86%2Fngx-http-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonicoder86%2Fngx-http-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sonicoder86","download_url":"https://codeload.github.com/sonicoder86/ngx-http-test/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223896997,"owners_count":17221475,"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-10T00:06:55.480Z","updated_at":"2024-11-10T00:06:56.430Z","avatar_url":"https://github.com/sonicoder86.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ngx-http-test\n[![Build Status](https://travis-ci.org/blacksonic/ngx-http-test.svg?branch=master)](https://travis-ci.org/blacksonic/ngx-http-test)\n[![Dependency Status](https://david-dm.org/blacksonic/ngx-http-test.svg)](https://david-dm.org/blacksonic/ngx-http-test)\n[![devDependency Status](https://david-dm.org/blacksonic/ngx-http-test/dev-status.svg)](https://david-dm.org/blacksonic/ngx-http-test?type=dev)\n[![peerDependency Status](https://david-dm.org/blacksonic/ngx-http-test/peer-status.svg)](https://david-dm.org/blacksonic/ngx-http-test?type=peer)\n\nMakes testing Http calls in Angular 2 as easy as were in AngularJS.\n\n### Installation\n\n```bash\nnpm install ngx-http-test --save-dev\n```\n\n### Usage\n\n```typescript\n// First add it as a provider\nbeforeEach(() =\u003e {\n  TestBed.configureTestingModule({\n    providers: [\n      ...\n      FakeBackend.getProviders()\n    ]\n  });\n});\n\n// Get the instance of it\nbeforeEach(inject([..., FakeBackend], (..., fakeBackend: FakeBackend) =\u003e {\n  backend = fakeBackend;\n}));\n\n// Use it in a test case\nit('should call fake endpoint', (done) =\u003e {\n  backend.expectGET('users/blacksonic').respond({ username: 'blacksonic' });\n  \n  subject.getProfile('blacksonic').subscribe((response) =\u003e {\n    expect(response).toEqual({ username: 'blacksonic' });\n    done();\n  });\n})\n```\n\nIt is possible to specify every detail of the response.\n\n```typescript\n// can be object, it will be json stringified if not a string\nlet responseBody = { username: 'blacksonic' };\nlet responseStatus = 200;\nlet responseHeaders = { 'Content-Type': 'application/json' };\n\nbackend\n  .expectGET('users/blacksonic')\n  .respond(responseBody, responseStatus, responseHeaders);\n```\n\nIt is not necessary to give the response, in that case the backend will respond with 200 empty response.\n\nAlso the expected url can be given as a regular expression instead of a string.\n\nFor requests outside of GET the request body can be also specified.\n\n```typescript\nbackend.expectPost(\n  'usernamepassword/login', // url\n  { username: 'blacksonic', password: 'secret' }, // payload\n  { 'Content-Type': 'application/json' } // headers\n).respond(responseForm);\n```\n\nConvenience methods available for different kind of requests: \n```expectGet```, ```expectPost```, ```expectDelete```, ```expectPut```, ```expectPatch```, ```expectHead```, ```expectOptions```.\n\nAfter the tests run it is possible to check for outstanding connections or expectations that are not addressed.\n\n```typescript\nafterEach(() =\u003e {\n  backend.verifyNoPendingRequests();\n  backend.verifyNoPendingExpectations();\n});\n```\n\nBy default expectations get verified instantly, but this can be switched off and do the verification by hand.\n\n```typescript\nit('should call fake endpoint', (done) =\u003e {\n  backend.setAutoRespond(false);\n  backend.expectGET('users/blacksonic').respond({ username: 'blacksonic' });\n  \n  subject.getProfile('blacksonic').subscribe((response) =\u003e {\n    expect(response).toEqual({ username: 'blacksonic' });\n    done();\n  });\n  \n  backend.flush();\n})\n```\n\nThe ```flush``` method resolves all pending connections. It can also be resolved one by one with the ```flushNext``` method.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonicoder86%2Fngx-http-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsonicoder86%2Fngx-http-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonicoder86%2Fngx-http-test/lists"}