-
-
Notifications
You must be signed in to change notification settings - Fork 240
feat(Http): can optionally fetch local resources from app bundle with Http #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vakrilov
merged 1 commit into
NativeScript:master
from
NathanWalker:http-local-semantics
Jun 23, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
nativescript-angular/file-system/ns-file-system.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import {Injectable} from '@angular/core'; | ||
| import {knownFolders, Folder} from 'file-system'; | ||
|
|
||
| // Allows greater flexibility with `file-system` and Angular | ||
| // Also provides a way for `file-system` to be mocked for testing | ||
|
|
||
| @Injectable() | ||
| export class NSFileSystem { | ||
| public currentApp(): Folder { | ||
| return knownFolders.currentApp(); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
nativescript-angular/http/ns-http.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import {Injectable} from '@angular/core'; | ||
| import {Http, XHRConnection, ConnectionBackend, RequestOptions, RequestOptionsArgs, ResponseOptions, ResponseType, Response, Request, BrowserXhr} from '@angular/http'; | ||
| import {Observable} from 'rxjs/Observable'; | ||
| import 'rxjs/add/observable/fromPromise'; | ||
| import {NSFileSystem} from '../file-system/ns-file-system'; | ||
|
|
||
| export class NSXSRFStrategy { | ||
| public configureRequest(req: any) { | ||
| // noop | ||
| } | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class NSHttp extends Http { | ||
| constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private nsFileSystem: NSFileSystem) { | ||
| super(backend, defaultOptions); | ||
| } | ||
|
|
||
| /** | ||
| * Performs a request with `get` http method. | ||
| * Uses a local file if `~/` resource is requested. | ||
| */ | ||
| get(url: string, options?: RequestOptionsArgs): Observable<Response | any> { | ||
| if (url.indexOf('~') === 0 || url.indexOf('/') === 0) { | ||
| // normalize url | ||
| url = url.replace('~', '').replace('/', ''); | ||
| // request from local app resources | ||
| return Observable.fromPromise(new Promise((resolve, reject) => { | ||
| let app = this.nsFileSystem.currentApp(); | ||
| let localFile = app.getFile(url); | ||
| if (localFile) { | ||
| localFile.readText().then((data) => { | ||
| resolve(responseOptions(data, 200, url)); | ||
| }, (err: Object) => { | ||
| reject(responseOptions(err, 400, url)); | ||
| }); | ||
| } else { | ||
| reject(responseOptions('Not Found', 404, url)); | ||
| } | ||
| })); | ||
| } else { | ||
| return super.get(url, options); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function responseOptions(body: string | Object, status: number, url: string): Response { | ||
| return new Response(new ResponseOptions({ | ||
| body: body, | ||
| status: status, | ||
| statusText: 'OK', | ||
| type: status === 200 ? ResponseType.Default : ResponseType.Error, | ||
| url: url | ||
| })); | ||
| } | ||
|
|
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
ng-sample/app/examples/http/data.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "results": [ | ||
| { | ||
| "title": "Test", | ||
| "description": "Testing Http local and remote." | ||
| } | ||
| ] | ||
| } |
50 changes: 50 additions & 0 deletions
ng-sample/app/examples/http/http-test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import {Component} from '@angular/core'; | ||
| import {Http} from '@angular/http'; | ||
| import 'rxjs/add/operator/map'; | ||
|
|
||
| /* IMPORTANT | ||
| In order to test out the full image example, to fix the App Transport Security error in iOS 9, you will need to follow this after adding the iOS platform: | ||
|
|
||
| https://blog.nraboy.com/2015/12/fix-ios-9-app-transport-security-issues-in-nativescript/ | ||
| */ | ||
|
|
||
| @Component({ | ||
| selector: 'http-test', | ||
| template: ` | ||
| <StackLayout horizontalAlignment="center"> | ||
| <Button text="Load Local File with Http" (tap)='loadLocal()' cssClass="btn-primary"></Button> | ||
| <Button text="Load Remote File with Http" (tap)='loadRemote()' cssClass="btn-primary"></Button> | ||
| <Label [text]="title" textWrap="true"></Label> | ||
| <Label [text]="description" textWrap="true"></Label> | ||
| </StackLayout> | ||
| `, | ||
| styles: [ | ||
| `Button { | ||
| margin-bottom:20; | ||
| }` | ||
| ] | ||
| }) | ||
| export class HttpTest { | ||
| public title: string; | ||
| public description: string; | ||
|
|
||
| constructor(private http: Http) { | ||
|
|
||
| } | ||
|
|
||
| public loadLocal() { | ||
| this.http.get('~/examples/http/data.json').map(res => res.json()).subscribe((response: any) => { | ||
| let user = response.results[0]; | ||
| this.title = user.title; | ||
| this.description = user.description; | ||
| }); | ||
| } | ||
|
|
||
| public loadRemote() { | ||
| this.http.get(`https://randomuser.me/api/?results=1&nat=us`).map(res => res.json()).subscribe((response: any) => { | ||
| let user = response.results[0]; | ||
| this.title = user.name.first; | ||
| this.description = user.email; | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
tests/app/tests/http.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| //make sure you import mocha-config before @angular/core | ||
| import {assert} from "./test-config"; | ||
| import { | ||
| async, | ||
| inject, | ||
| beforeEach, | ||
| beforeEachProviders | ||
| } from '@angular/core/testing'; | ||
| import {provide, ReflectiveInjector} from '@angular/core'; | ||
| import {BaseRequestOptions, ConnectionBackend, Http, HTTP_PROVIDERS, Response, ResponseOptions} from '@angular/http'; | ||
| import 'rxjs/add/operator/map'; | ||
| import {MockBackend} from '@angular/http/testing'; | ||
| import {NSHttp} from "nativescript-angular/http/ns-http"; | ||
| import {NSFileSystem} from "nativescript-angular/file-system/ns-file-system"; | ||
| import {NSFileSystemMock, FileResponses} from './mocks/ns-file-system.mock'; | ||
|
|
||
| describe("Http", () => { | ||
| let http: Http; | ||
| let backend: MockBackend; | ||
|
|
||
| beforeEach(() => { | ||
| let injector = ReflectiveInjector.resolveAndCreate([ | ||
| HTTP_PROVIDERS, | ||
| BaseRequestOptions, | ||
| MockBackend, | ||
| provide(NSFileSystem, { useClass: NSFileSystemMock }), | ||
| provide(Http, { | ||
| useFactory: function (backend: ConnectionBackend, defaultOptions: BaseRequestOptions, nsFileSystem: NSFileSystem) { | ||
| return new NSHttp(backend, defaultOptions, nsFileSystem); | ||
| }, | ||
| deps: [MockBackend, BaseRequestOptions, NSFileSystem] | ||
| }) | ||
| ]); | ||
|
|
||
| backend = injector.get(MockBackend); | ||
| http = injector.get(Http); | ||
| }); | ||
|
|
||
| it("should work with local files prefixed with '~'", () => { | ||
| http.get('~/test.json').map(res => res.json()).subscribe((response: any) => { | ||
| assert.strictEqual(3, response.length); | ||
| assert.strictEqual('Alex', response[0].name); | ||
| }); | ||
| }); | ||
|
|
||
| it("should work with local files prefixed with '/'", () => { | ||
| http.get('/test.json').map(res => res.json()).subscribe((response: any) => { | ||
| assert.strictEqual(3, response.length); | ||
| assert.strictEqual('Panayot', response[2].name); | ||
| }); | ||
| }); | ||
|
|
||
| it("should work with remote files", () => { | ||
| let connection: any; | ||
| backend.connections.subscribe((c: any) => connection = c); | ||
| http.get('http://www.nativescript.org/test.json').map(res => res.json()).subscribe((response: any) => { | ||
| assert.strictEqual(3, response.length); | ||
| assert.strictEqual('Rosen', response[1].name); | ||
| }); | ||
| connection.mockRespond(new Response(new ResponseOptions({ body: FileResponses.AWESOME_TEAM }))); | ||
| }); | ||
| }); |
35 changes: 35 additions & 0 deletions
tests/app/tests/mocks/ns-file-system.mock.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import {Injectable} from '@angular/core'; | ||
| import {ResponseType, Response, ResponseOptions} from '@angular/http'; | ||
|
|
||
| export class FileResponses { | ||
| public static AWESOME_TEAM: string = '[{"name":"Alex"}, {"name":"Rosen"}, {"name":"Panayot"}]'; | ||
| } | ||
|
|
||
| // Folder mock | ||
| class Folder { | ||
| public getFile(url: string): any { | ||
| let data; | ||
| switch (url) { | ||
| case 'test.json': | ||
| data = FileResponses.AWESOME_TEAM; | ||
| break; | ||
| default: | ||
| throw (new Error('Unsupported file for the testing mock - ns-file-system-mock')); | ||
| } | ||
| return { | ||
| readText: () => { | ||
| return new Promise((resolve) => { | ||
| resolve(data); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Filesystem mock | ||
| @Injectable() | ||
| export class NSFileSystemMock { | ||
| public currentApp(): Folder { | ||
| return new Folder(); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
tests/app/tests/xhr-paths.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.