Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Testing the malformed JSON server responses with ngMock does not work anymore since 1.3.0-rc.5 #11433

Closed
@kayhadrin

Description

@kayhadrin

Problem definition

Given I have an angular app using $http to do ajax requests
When my app makes an ajax call
And the server response is:
   - status=200
   - content-type=application/json
   - data= invalid JSON string
Then I see that the $http error handler is called

Context

This issue surfaced after commit 7b6c1d0 where several people already exposed their concerns about the side-effects of that change.

Here's a test case that shows how it used to work before 1.3.0-rc.5: http://codepen.io/kayhadrin/pen/Joxxrd

Log output:

pen.js:9 angular.version: =  1.3.0-rc.4
pen.js:46 test: good response
pen.js:52 mock response: arguments =  ["GET", "/test1", undefined, Object]
pen.js:54 mock response: ret =  [200, "{"result": "some message"}"]
pen.js:19 received(/test1): success:  {"result":"some message"}
pen.js:62 test: bad response 1: http error status
pen.js:68 mock response: arguments =  ["GET", "/test2", undefined, Object]
pen.js:74 mock response: ret =  [500, "{"error": "some message"}", Object]
pen.js:24 received(/test2): failure:  Object {data: Object, status: 500, headers: function, config: Object, statusText: ""}config: Objectdata: Objecterror: "some message"__proto__: Objectheaders: function (name) {status: 500statusText: ""__proto__: Object
pen.js:82 test: bad response 2: invalid json string
pen.js:88 mock response: arguments =  ["GET", "/test3", undefined, Object]
pen.js:94 mock response: ret =  [200, "dummy data", Object]
pen.js:21 received(/test3): failure: invalid response data:  "dummy data"
pen.js:104 $rootScope.$apply() still works

And this one shows how it breaks test cases after: http://codepen.io/kayhadrin/pen/RNvvGo

Log output:

angular.version: =  1.3.15
pen.js:46 test: good response
pen.js:52 mock response: arguments =  ["GET", "/test1", undefined, Object]
pen.js:54 mock response: ret =  [200, "{"result": "some message"}"]
pen.js:19 received(/test1): success:  {"result":"some message"}
pen.js:62 test: bad response 1: http error status
pen.js:68 mock response: arguments =  ["GET", "/test2", undefined, Object]
pen.js:74 mock response: ret =  [500, "{"error": "some message"}", Object]
pen.js:24 received(/test2): failure:  Object {data: Object, status: 500, headers: function, config: Object, statusText: ""}
pen.js:82 test: bad response 2: invalid json string
pen.js:88 mock response: arguments =  ["GET", "/test3", undefined, Object]
pen.js:94 mock response: ret =  [200, "dummy data", Object]
pen.js:42 flushServerResponse error:
 SyntaxError: Unexpected token d
    at Object.parse (native)
    at fromJson (https://code.angularjs.org/1.3.15/angular.js:1072:14)
    at defaultHttpResponseTransform (https://code.angularjs.org/1.3.15/angular.js:8618:16)
    at https://code.angularjs.org/1.3.15/angular.js:8703:12
    at forEach (https://code.angularjs.org/1.3.15/angular.js:323:20)
    at transformData (https://code.angularjs.org/1.3.15/angular.js:8702:3)
    at transformResponse (https://code.angularjs.org/1.3.15/angular.js:9428:23)
    at processQueue (https://code.angularjs.org/1.3.15/angular.js:13248:27)
    at https://code.angularjs.org/1.3.15/angular.js:13264:27
    at Scope.$get.Scope.$eval (https://code.angularjs.org/1.3.15/angular.js:14466:28)pen.js:42 flushServerResponsepen.js:100 (anonymous function)pen.js:112 (anonymous function)
pen.js:24 received(/test3): failure:  SyntaxError: Unexpected token d {stack: (...), message: "Unexpected token d"}
pen.js:107 $rootScope.$apply() does not work anymore.
 Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.3.15/$rootScope/inprog?p0=%24digest
    at REGEX_STRING_REGEXP (https://code.angularjs.org/1.3.15/angular.js:63:12)
    at beginPhase (https://code.angularjs.org/1.3.15/angular.js:14820:15)
    at Scope.$get.Scope.$apply (https://code.angularjs.org/1.3.15/angular.js:14564:11)
    at http://s.codepen.io/boomerang/8792b8e09b609dcec2a546a6abc7b92a1427352813876/index.html:116:29
    at http://s.codepen.io/boomerang/8792b8e09b609dcec2a546a6abc7b92a1427352813876/index.html:125:4

General expectation

I think that angular should:

  • detect the JSON parsing issue (as it does already) and redirect the error to the $http error handler (callback and/or promises)
  • not break the $digest cycle so that the rest of the app can work normally (at least not disrupt $digest when the $http rejected promise handler could resolve the issue)

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions