Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Dspace7 Angular
Manage
Activity
Members
Labels
Plan
Issues
42
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
code.vt.edu will be down for maintenance from 0530-0630 EDT Wednesday, March 26th
Show more breadcrumbs
vtechworks
Dspace7 Angular
Commits
1bf239f8
Commit
1bf239f8
authored
5 years ago
by
lotte
Browse files
Options
Downloads
Patches
Plain Diff
61873: content type fallback
parent
f54a3a4d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
+59
-15
59 additions, 15 deletions
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts
+51
-10
51 additions, 10 deletions
src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts
with
110 additions
and
25 deletions
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
+
59
−
15
View file @
1bf239f8
...
...
@@ -3,6 +3,8 @@ import { HttpClientTestingModule, HttpTestingController } from '@angular/common/
import
{
DSpaceRESTv2Service
}
from
'
./dspace-rest-v2.service
'
;
import
{
DSpaceObject
}
from
'
../shared/dspace-object.model
'
;
import
{
RestRequestMethod
}
from
'
../data/rest-request-method
'
;
import
{
HttpHeaders
}
from
'
@angular/common/http
'
;
describe
(
'
DSpaceRESTv2Service
'
,
()
=>
{
let
dSpaceRESTv2Service
:
DSpaceRESTv2Service
;
...
...
@@ -47,29 +49,71 @@ describe('DSpaceRESTv2Service', () => {
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
method
).
toBe
(
'
GET
'
);
req
.
flush
(
mockPayload
,
{
status
:
mockStatusCode
,
statusText
:
mockStatusText
});
req
.
flush
(
mockPayload
,
{
status
:
mockStatusCode
,
statusText
:
mockStatusText
});
});
it
(
'
should throw an error
'
,
()
=>
{
dSpaceRESTv2Service
.
get
(
url
).
subscribe
(()
=>
undefined
,
(
err
)
=>
{
expect
(
err
).
toEqual
(
mockError
);
});
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
method
).
toBe
(
'
GET
'
);
req
.
error
(
mockError
);
});
});
it
(
'
should throw an error
'
,
()
=>
{
dSpaceRESTv2Service
.
get
(
url
).
subscribe
(()
=>
undefined
,
(
err
)
=>
{
expect
(
err
).
toEqual
(
mockError
);
it
(
'
should log an error
'
,
()
=>
{
spyOn
(
console
,
'
log
'
);
dSpaceRESTv2Service
.
get
(
url
).
subscribe
(()
=>
undefined
,
(
err
)
=>
{
expect
(
console
.
log
).
toHaveBeenCalled
();
});
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
method
).
toBe
(
'
GET
'
);
req
.
error
(
mockError
);
});
it
(
'
when no content-type header is provided, it should use application/json
'
,
()
=>
{
dSpaceRESTv2Service
.
request
(
RestRequestMethod
.
POST
,
url
,
{}).
subscribe
();
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
headers
.
get
(
'
Content-Type
'
)).
toContain
(
'
application/json; charset=utf-8
'
);
});
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
method
).
toBe
(
'
GET
'
);
req
.
error
(
mockError
);
});
it
(
'
should log an error
'
,
()
=>
{
spyOn
(
console
,
'
log
'
);
describe
(
'
#request
'
,
()
=>
{
it
(
'
should return an Observable<DSpaceRESTV2Response>
'
,
()
=>
{
const
mockPayload
=
{
page
:
1
};
const
mockStatusCode
=
200
;
const
mockStatusText
=
'
GREAT
'
;
dSpaceRESTv2Service
.
request
(
RestRequestMethod
.
POST
,
url
,
{}).
subscribe
((
response
)
=>
{
expect
(
response
).
toBeTruthy
();
expect
(
response
.
statusCode
).
toEqual
(
mockStatusCode
);
expect
(
response
.
statusText
).
toEqual
(
mockStatusText
);
expect
(
response
.
payload
.
page
).
toEqual
(
mockPayload
.
page
);
});
dSpaceRESTv2Service
.
get
(
url
).
subscribe
(()
=>
undefined
,
(
err
)
=>
{
expect
(
console
.
log
).
toHaveBeenCalled
();
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
method
).
toBe
(
'
POST
'
);
req
.
flush
(
mockPayload
,
{
status
:
mockStatusCode
,
statusText
:
mockStatusText
});
});
it
(
'
when a content-type header is provided, it should not use application/json
'
,
()
=>
{
const
headers
=
new
HttpHeaders
({
'
Content-Type
'
:
'
text/html
'
});
dSpaceRESTv2Service
.
request
(
RestRequestMethod
.
POST
,
url
,
{},
{
headers
}).
subscribe
();
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
headers
.
get
(
'
Content-Type
'
)).
not
.
toContain
(
'
application/json; charset=utf-8
'
);
});
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
method
).
toBe
(
'
GET
'
);
req
.
error
(
mockError
);
it
(
'
when no content-type header is provided, it should use application/json
'
,
()
=>
{
dSpaceRESTv2Service
.
request
(
RestRequestMethod
.
POST
,
url
,
{}).
subscribe
();
const
req
=
httpMock
.
expectOne
(
url
);
expect
(
req
.
request
.
headers
.
get
(
'
Content-Type
'
)).
toContain
(
'
application/json; charset=utf-8
'
);
});
});
describe
(
'
buildFormData
'
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts
+
51
−
10
View file @
1bf239f8
import
{
throwError
as
observableThrowError
,
Observable
}
from
'
rxjs
'
;
import
{
catchError
,
map
}
from
'
rxjs/operators
'
;
import
{
Observable
,
throwError
as
observableThrowError
}
from
'
rxjs
'
;
import
{
catchError
,
map
}
from
'
rxjs/operators
'
;
import
{
Injectable
}
from
'
@angular/core
'
;
import
{
HttpClient
,
HttpHeaders
,
HttpParams
,
HttpResponse
}
from
'
@angular/common/http
'
...
...
@@ -8,6 +8,7 @@ import { HttpObserve } from '@angular/common/http/src/client';
import
{
RestRequestMethod
}
from
'
../data/rest-request-method
'
;
import
{
isNotEmpty
}
from
'
../../shared/empty.util
'
;
import
{
DSpaceObject
}
from
'
../shared/dspace-object.model
'
;
import
{
cloneDeep
}
from
'
lodash
'
;
export
interface
HttpOptions
{
body
?:
any
;
...
...
@@ -38,11 +39,23 @@ export class DSpaceRESTv2Service {
* An Observable<string> containing the response from the server
*/
get
(
absoluteURL
:
string
):
Observable
<
DSpaceRESTV2Response
>
{
return
this
.
http
.
get
(
absoluteURL
,
{
observe
:
'
response
'
}).
pipe
(
map
((
res
:
HttpResponse
<
any
>
)
=>
({
payload
:
res
.
body
,
statusCode
:
res
.
status
,
statusText
:
res
.
statusText
})),
const
requestOptions
=
{
observe
:
'
response
'
as
any
,
headers
:
new
HttpHeaders
({
'
Content-Type
'
:
'
application/json; charset=utf-8
'
})
};
return
this
.
http
.
get
(
absoluteURL
,
requestOptions
).
pipe
(
map
((
res
:
HttpResponse
<
any
>
)
=>
({
payload
:
res
.
body
,
statusCode
:
res
.
status
,
statusText
:
res
.
statusText
})),
catchError
((
err
)
=>
{
console
.
log
(
'
Error:
'
,
err
);
return
observableThrowError
({
statusCode
:
err
.
status
,
statusText
:
err
.
statusText
,
message
:
err
.
message
});
return
observableThrowError
({
statusCode
:
err
.
status
,
statusText
:
err
.
statusText
,
message
:
err
.
message
});
}));
}
...
...
@@ -65,17 +78,45 @@ export class DSpaceRESTv2Service {
requestOptions
.
body
=
this
.
buildFormData
(
body
);
}
requestOptions
.
observe
=
'
response
'
;
if
(
options
&&
options
.
headers
)
{
requestOptions
.
headers
=
Object
.
assign
(
new
HttpHeaders
(),
options
.
headers
);
}
if
(
options
&&
options
.
responseType
)
{
requestOptions
.
responseType
=
options
.
responseType
;
}
// WORKING OPTION
// requestOptions.headers = ((options && options.headers) || new HttpHeaders()).set('blaat', 'bla').delete('blaat');
// OPTION 1
// requestOptions.headers = ((options && options.headers) || new HttpHeaders());
// OPTION 2
// requestOptions.headers = new HttpHeaders();
// if (options && options.headers) {
// options.headers.keys().forEach((key) => {
// options.headers.getAll(key).forEach((header) => {
// // Because HttpHeaders is immutable, the set method returns a new object instead of updating the existing headers
// requestOptions.headers = requestOptions.headers.set(key, header);
// })
// });
// }
//
// if (!requestOptions.headers.has('Content-Type')) {
// // Because HttpHeaders is immutable, the set method returns a new object instead of updating the existing headers
// requestOptions.headers = requestOptions.headers.set('Content-Type', 'application/json; charset=utf-8');
// }
return
this
.
http
.
request
(
method
,
url
,
requestOptions
).
pipe
(
map
((
res
)
=>
({
payload
:
res
.
body
,
headers
:
res
.
headers
,
statusCode
:
res
.
status
,
statusText
:
res
.
statusText
})),
map
((
res
)
=>
({
payload
:
res
.
body
,
headers
:
res
.
headers
,
statusCode
:
res
.
status
,
statusText
:
res
.
statusText
})),
catchError
((
err
)
=>
{
console
.
log
(
'
Error:
'
,
err
);
return
observableThrowError
({
statusCode
:
err
.
status
,
statusText
:
err
.
statusText
,
message
:
err
.
message
});
return
observableThrowError
({
statusCode
:
err
.
status
,
statusText
:
err
.
statusText
,
message
:
err
.
message
});
}));
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment