170 lines
4.1 KiB
Plaintext
170 lines
4.1 KiB
Plaintext
|
|
p.location-badge.
|
|
exported from <a href='../http'>angular2/http</a>
|
|
defined in <a href="https://github.com/angular/angular/tree/2.0.0-alpha.37/modules/angular2/src/http/backends/mock_backend.ts#L98-L226">angular2/src/http/backends/mock_backend.ts (line 98)</a>
|
|
|
|
:markdown
|
|
The http module provides services to perform http requests. To get started, see the <a href='Http-class.html'><code>Http</code></a>
|
|
class.
|
|
|
|
.l-main-section
|
|
h2 Annotations
|
|
.l-sub-section
|
|
h3.annotation Injectable
|
|
pre.prettyprint
|
|
code.
|
|
@Injectable()
|
|
|
|
|
|
.l-main-section
|
|
h2 Members
|
|
.l-sub-section
|
|
h3 constructor
|
|
|
|
|
|
pre.prettyprint
|
|
code.
|
|
constructor()
|
|
|
|
:markdown
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
h3 connections
|
|
|
|
|
|
:markdown
|
|
<a href='../core/EventEmitter-class.html'><code>EventEmitter</code></a>
|
|
of <a href='MockConnection-class.html'><code>MockConnection</code></a> instances that have been created by this backend. Can be subscribed
|
|
to in order to respond to connections.
|
|
|
|
#Example
|
|
|
|
```
|
|
import {MockBackend, Http, BaseRequestOptions} from 'angular2/http';
|
|
import {Injector} from 'angular2/core';
|
|
|
|
it('should get a response', () => {
|
|
var connection; //this will be set when a new connection is emitted from the backend.
|
|
var text; //this will be set from mock response
|
|
var injector = Injector.resolveAndCreate([
|
|
MockBackend,
|
|
bind(Http).toFactory(backend, options) {
|
|
return new Http(backend, options);
|
|
}, [MockBackend, BaseRequestOptions]]);
|
|
var backend = injector.get(MockBackend);
|
|
var http = injector.get(Http);
|
|
backend.connections.toRx().subscribe(c => connection = c);
|
|
http.request('something.json').toRx().subscribe(res => {
|
|
text = res.text();
|
|
});
|
|
connection.mockRespond(new Response({body: 'Something'}));
|
|
expect(text).toBe('Something');
|
|
});
|
|
```
|
|
|
|
This property only exists in the mock implementation, not in real Backends.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
h3 connectionsArray
|
|
|
|
|
|
:markdown
|
|
An array representation of `connections`. This array will be updated with each connection that
|
|
is created by this backend.
|
|
|
|
This property only exists in the mock implementation, not in real Backends.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
h3 pendingConnections
|
|
|
|
|
|
:markdown
|
|
<a href='../core/EventEmitter-class.html'><code>EventEmitter</code></a> of <a href='MockConnection-class.html'><code>MockConnection</code></a> instances that haven't yet been resolved (i.e.
|
|
with a `readyState`
|
|
less than 4). Used internally to verify that no connections are pending via the
|
|
`verifyNoPendingRequests` method.
|
|
|
|
This property only exists in the mock implementation, not in real Backends.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
h3 verifyNoPendingRequests
|
|
|
|
|
|
pre.prettyprint
|
|
code.
|
|
verifyNoPendingRequests()
|
|
|
|
:markdown
|
|
Checks all connections, and raises an exception if any connection has not received a response.
|
|
|
|
This method only exists in the mock implementation, not in real Backends.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
h3 resolveAllConnections
|
|
|
|
|
|
pre.prettyprint
|
|
code.
|
|
resolveAllConnections()
|
|
|
|
:markdown
|
|
Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve
|
|
connections, if it's expected that there are connections that have not yet received a response.
|
|
|
|
This method only exists in the mock implementation, not in real Backends.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.l-sub-section
|
|
h3 createConnection
|
|
|
|
|
|
pre.prettyprint
|
|
code.
|
|
createConnection(req: Request)
|
|
|
|
:markdown
|
|
Creates a new <a href='MockConnection-class.html'><code>MockConnection</code></a>. This is equivalent to calling `new
|
|
MockConnection()`, except that it also will emit the new `Connection` to the `connections`
|
|
emitter of this `MockBackend` instance. This method will usually only be used by tests
|
|
against the framework itself, not by end-users.
|
|
|
|
|
|
|
|
|
|
|
|
|