I have an app I am building, like most I have built in the last few years, with a clean separation: REST API on the back-end, Angular (before that Backbone, before that just jQuery) on the front-end.
When first testing, it is easy to stub the REST API; when developing, I can do things like a static file server (like angular-seed's ./scripts/web-server) and put in place files. But at a certain point, I really need to develop and test against the real REST API.
How do people build against and launch in a case of a separated app like this? In the past, I had a ./dev node script which would set all sorts of test/dev database variables, then require('../../server-dev-dir/app.js') but it seems sort of heavy. Is there a cleaner way to do it?
Same issue for testing: the app really needs to be tested against a true REST API, at a certain point, stubs and mocks only help so much.
In AngularJS, you make all of your HTTP requests using the $http service. It's not your job to test $http. That's the job of AngularJS team. All you care about is this:
does your back-end or 3rd party (API) back-end receive the requests that $http is sending as configured by your AngularJS code?
To that end, AngularJS provides the $httpBackend mock. When angular-mocks.js is loaded in your tests, $http will send HTTP requests to it. You can train $httpBackend on how to respond to any URL.
You can also make assertions on what requests $httpBackend received and what data it received with those requests.
Related
I'm building a React client and a REST server with Django REST framework, neither of which are something I'm particularly good at.
Anyway, on the client side, I find myself writing various calls to fetch, and managing the endpoints and content types, which seems dumb.
I noticed that I can use CoreAPI to build a schema of my API and somehow automagically make an API client.
Can I, and would it be sensible and straitforward, to somehow autobuild that client and inject it into my app, and then call it.
This sounds nice and feasible, but not sure how to go about it, thoughts?
Thanks.
The best library I have found for writing js client for DRF is https://github.com/tulios/mappersmith
I'm working with a JavaScript framework (wakanda) in back-end that uses XHR requests, and with AngularJS in front-end.
I used angular-loading-bar that works perfectly and automatically with HTTP requests, but it doesn't work automatically with XHR requests.
is there any way to make it work AUTOMATICALLY XHR?
XHR vs HTTP
First XHR stands for the "XmlHTTPRequest API" ().
(see also the W3C spec version & the Mozilla documentation)
This API, introduced by Microsoft, then standardised by WHATWG & W3C,facilitated the creation of Ajax based Websites and Web Applications.
When you make an XHR request, it sends an HTTP request
$http vs HTTP
angular-loading-bar use an angularJS $http interceptor to listen all HTTP requests done using the angular $http API.
The angular $http API is using XHR to make its HTTP requests, but it does not overwrite XMLHttpRequest nor it does for its prototype. So it can not intercept HTTP requests made from code using directly XHR.
angular-wakanda HTTP requests
angula-wakanda is an angular service written on top of the Wakanda Framework data provider. It can then more easily update itself with new features that are occasionally added to the Wakanda HTTP (REST, JSON-RPC) APIs and/or to the Wakanda data provider one.
As this data provider is framework agnostic (can be used with dojo, backbone, or anything else), it does not use the angular specific $http API and its HTTP requests are then not intercepted by the angular-loading-bar
How to fix it?
Introducing low-level adapters layer for the Wakanda data-provider is something that have been discussed by the Wakanda team. It might then allow to choose which HTTP API to use in some dedicated contexts (as in nodejs where XHR is not the native HTTP API). There should be a "data provider" dedicated github repository added to the Github Wakanda Team Account in the future. In the meantime, you can send a feature request to the Angular-Wakanda Github repository (do not hesitate to mention this Stack overflow answer), and why not, also try to do a fork for a proposal ;-)
I want to know how to work with angularjs without using a backend. I know the option of using $httpBackend and mock, but I want a real life example.
In my team we are considering the possibility of using an express server that simply works with JSON files (no database) If there is no alternative.
Any recommendations? Thanks in advance.
For tests or to begin work when the backend is not ready yet, I often use services to mock REST or SOAP API, such as: www.mockable.io.
Otherwise you can have a simple server to serve static JSON files.
I have a backend REST app. I'm presently developing the frontend app. Now i have a confusion as to how to setup the frontend app.
Case 1 - Plan to use Spring and have a frontend controller layer that takes care of calling the REST services. But i need to have models and POJOs setup same as in the backend to parse the JSON response. This seems like a overload on frontend. How to go around about this?
Case 2 - Plan to use Angular.js. Then i need to have all my REST URLs in the controller.js which is completely accessible for anyone. That way im totally exposing my REST domain, URL and request format. Is it not a security threat? How to go around about this?
Can you please tell me which case is better and secure and how to resolve the problem attached with it?
Secure your REST endpoints using a scheme like OpenId or OAuth or something else. Spring and numerous other web app frameworks have components to help with such authentication.
AngularJS is a client side framework. You can use Angular along with Spring. They are not mutually exclusive.
Finally, any http request (including RESTful http requests) invoked on a client is easily accessed simply by viewing the network traffic. Chrome, Firefox, along with other browsers provide tools, out of the box, that make this very easy to do. All the more reason to secure those REST endpoints.
I am tasked with writing an AngularJS app that connects to a RESTful API provided by our client. I want to wrap that API in an angular service and write tests against it using the real API. I am confused on how and where to do this.
Should I be using Protractor for this test? It doesn't involve any UX elements at all, so it doesn't feel like it should use Protractor.
Should I be using Karma? I tried Karma with $httpBackend, ngMockE2E and .passThrough() for all whenGET and whenPOST calls, but I'm having issues with Unexpected request: POST errors.
You should use Karma. Protractor is for testing interaction with web pages.
Your Unexpected request: POST errors should be resolvable if you use $httpBackend.expectPOST() for each POST request you issue.