Pass parameters through angularjs 2 to restful services - web-services

I am trying to call a webservice which is as follows,
#RequestMapping(value = { "/persons" },method = RequestMethod.GET,headers="Accept=application/json")
public List<Person> getDummyData(#RequestParam(value="search", defaultValue="a") String search,HttpServletRequest req){
List<Person> listOfMatchedPersons=listOfPersons.stream().filter(person->person.getName().contains(search)).collect(Collectors.toList());
req.getParameterMap().forEach((k,v)->System.out.println(k+" : "+v));
return listOfMatchedPersons;
}
I want to call this service with some parameter from my UI, but it always executes this method with default value of search i.e. a.
Following is my angularjs 2's service that is consuming this service,
search(term: string) {
var params = new URLSearchParams();
params.set('search', term);
let aopsServices = 'http://localhost:8080/dummy/persons';//?search='+term;
this.ot = this.http
.get(aopsServices,params)
.map(response => response.json())
;
return this.ot;
}
however if i change the url to http://localhost:8080/dummy/persons'?search='+term; it works.
And also what should be the ideal approach to access the restful services if they are secured ?

I see two ways to do that:
Leveraging the URLSearchParams class:
search(term: string) {
var params = new URLSearchParams();
params.set('search', term);
let aopsServices = 'http://localhost:8080/dummy/persons';
this.ot = this.http
.get(aopsServices, { search: params })
.map(response => response.json());
return this.ot;
}
Use of ` (backticks) instead of single quotes '
search(term: string) {
let aopsServices = `http://localhost:8080/dummy/persons?search=${term}`;
this.ot = this.http
.get(aopsServices, { search: params })
.map(response => response.json());
return this.ot;
}
The second approach is more concise but doesn't urlencode the parameter.

I changed my code to
var params = new URLSearchParams();
params.set('search', term);
let aopsServices = 'http://localhost:8080/dummy/persons';
this.ot = this.http
.get(aopsServices,new RequestOptions({search:params}))
.map(response => response.json());
and it worked.
I misread the documentation of get.

Related

Unit testing sessionStorage value in emberJS

I'm new to ember and trying to figure out how to unit test, using sinon, the sessionStorage based on url parameters when that page is visited. I've tried a few things but still can't get the desired result. It passes even if I change the 'sessionValue' without editing the query param.
Thank you in advance.
ember component
beforeModel(transition) {
//transition will contain an object containing a query parameter. '?userid=1234' and is set in the sessionStorage.
if(transition.queryparam.hasOwnProperty('userid')){
sessionStorage.setItem('user:id', transition.queryparam)
}
}
Ember test
test('Session Storage contains query param value', async assert => {
let sessionKey = "user:id";
let sessionValue = "1234"
let store = {};
const mockLocalStorage = {
getItem: (key) => {
return key in store ? store[key] : null;
},
setItem: (key, value) => {
store[key] = `${value}`;
},
clear: () => {
store = {};
}
};
asserts.expect(1);
let spy = sinon.spy(sessionStorage, "setItem");
spy.calledWith(mockLocalStorage.setItem);
let stub = sinon.stub(sessionStorage, "getItem");
stub.calledWith(mockLocalStorage.getItem);
stub.returns(sessionValue);
await visit('/page?userid=1234');
mockLocalStorage.setItem(sessionKey, sessionValue);
assert.equal(mockLocalStorage.getItem(sessionKey), sessionValue, 'storage contains value');
})
Welcome to Ember!
There are many ways to test, and the below suggestion is one way (how I would approach interacting with the SessionStorage).
Instead of re-creating the SessionStorage API in your test, how do you feel about using a pre-made proxy around the Session Storage? (ie: "Don't mock what you don't own")
Using: https://github.com/CrowdStrike/ember-browser-services/#sessionstorage
Your app code would look like:
#service('browser/session-storage') sessionStorage;
beforeModel(transition) {
// ... details omitted ...
// note the addition of `this` -- the apis are entirely the same
// as SessionStorage
this.sessionStorage.setItem('user:id', ...)
}
then in your test:
module('Scenario Name', function (hooks) {
setupApplicationTest(hooks);
setupBrowserFakes(hooks, { sessionStorage: true });
test('Session Storage contains query param value', async assert => {
let sessionKey = "user:id";
let sessionValue = "1234"
let sessionStorage = this.owner.lookup('browser/session-storage');
await visit('/page?userid=1234');
assert.equal(sessionStorage.getItem(sessionKey), '1234', 'storage contains value');
});
})
With this approach, sinon isn't even needed :)

How to prevent window.open(url, '_blank') to actually open during jasmine unit test

When I'm trying to test a window.open(url, '_blank'), it'll automatically open a new tab in my browser during the testing. Is there any way I can prevent this from happening?
Like, try to open a new tab, but don't do it. I know you could do jasmine.createSpyObj('obj', ['methods']).and.returnValue({...}), but I'm not sure how to do this for window.open.
NavigationService
export class NavigationService {
navigate(url: string, openInNewTab: boolean = false): boolean {
if (url.startsWith('http')) {
openInNewTab ? window.open(url, '_blank') : ((window as any).location.href = url);
return false;
}
...
}
}
NavigationService spec
describe('NavigationService', () => {
let navigationService: NavigationService;
let routerSpy, configServiceSpy, windowSpy;
let httpUrlMock = 'http://mock.com';
beforeEach(() => {
routerSpy = jasmine.createSpyObj('Router', ['createUrlTree', 'navigateByUrl']);
configServiceSpy = jasmine.createSpyObj('ConfigService', ['current']);
windowSpy = spyOn(window, 'open').and.callThrough();
navigationService = new NavigationService(routerSpy, configServiceSpy);
});
it('should open the url in a new tab when the url starts with http ', () => {
let _result = navigationService.navigate(httpUrlMock, true);
expect(windowSpy).toHaveBeenCalledWith(httpUrlMock, '_blank');
});
}
The problem with your code is the using of callThrough method, which is responsible for opening the URL. Just remove it and it will resolve your issue.
Just use the below line:
windowSpy = spyOn(window, 'open');
Use of callthrough

Can not pass a list of strings to a Web API endpoint. Why?

Can not pass a list of strings to a Web API endpoint. Why?
Here is my controller:
[Route("[controller]")]
[ApiController]
public class MyController
{
[HttpPost("foo")]
public string MyMethod(List<string> strs)
{
return "foo";
}
}
Here is how I am trying to call it:
var strs = new List<string> { "bar" };
var json = JsonConvert.SerializeObject(strs);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpCliet.PostAsync("/My/foo", content);
Before calling the endpoint I place a breakpoint on the return "foo"; line. Once the breakpoint is hit the strs list inside the MyController.MyMethod is empty. The strs is not null, but it contains no elements. While my intentions and expectations are to see the strs containing one element, i.e. the string "bar".
I am using the ASP.NET Core 2.2 in project where I create and use the HttpClient. And I am using the same ASP.NET Core 2.2 in project where I have the endpoint.
I am not sure what is wrong here. I have checked a few sources. E.g. the following:
C# HTTP post , how to post with List<XX> parameter?
https://carldesouza.com/httpclient-getasync-postasync-sendasync-c/
https://blog.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/
And I can not find what I am missing according to those resources.
UPDATE
The following call works for me as expected:
var json = JsonConvert.SerializeObject(string.Empty);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await server.CreateClient().PostAsync("/My/foo?strs=bar", content);
Maybe someone knows why the parameters in my case are read from the query string only, but not from body?
You can change your url to a full url in client.PostAsync.Here is a demo worked:
Api(localhost:44379):
WeatherForecastController:
[HttpPost("foo")]
public string MyMethod(List<string> strs)
{
return "foo";
}
Call(localhost:44326):
public async Task<IActionResult> CheckAsync() {
HttpClient client = new HttpClient();
var strs = new List<string> { "bar","bar1","bar2" };
var json = JsonConvert.SerializeObject(strs);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://localhost:44379/WeatherForecast/foo", content);
return Ok(response);
}
result:

.net core Url.Action mock, how to?

How to mock Url.Action during testing controller action?
I'm trying to unit test my asp.net core controller action.
Logic of action has Url.Action and I need to mock it to complete test but I can't find right solution.
Thank you for your help!
UPDATE
this is my method in controller that I need to test.
public async Task<IActionResult> Index(EmailConfirmationViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Email);
if (user == null) return RedirectToAction("UserNotFound");
if (await _userManager.IsEmailConfirmedAsync(user)) return RedirectToAction("IsAlreadyConfirmed");
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("Confirm", "EmailConfirmation", new { userId = user.Id, token }, HttpContext.Request.Scheme);
await _emailService.SendEmailConfirmationTokenAsync(user, callbackUrl);
return RedirectToAction("EmailSent");
}
return View(model);
}
I have problem with mocking this part:
var callbackUrl = Url.Action("Confirm", "EmailConfirmation", new { userId = user.Id, token }, HttpContext.Request.Scheme);
Finally I found solution!
When you are mocking UrlHelper you need to mock only base method Url.Action(UrlActionContext context) because all helper methods actually use it.
var mockUrlHelper = new Mock<IUrlHelper>(MockBehavior.Strict);
mockUrlHelper
.Setup(
x => x.Action(
It.IsAny<UrlActionContext>()
)
)
.Returns("callbackUrl")
.Verifiable();
_controller.Url = mockUrlHelper.Object;
Also! I have problem because of null in HttpContext.Request.Scheme. You need to mock HttpContext
_controller.ControllerContext.HttpContext = new DefaultHttpContext();
I added
var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock
.Setup(x => x.Action(It.IsAny<UrlActionContext>()))
.Returns((UrlActionContext uac) =>
$"{uac.Controller}/{uac.Action}#{uac.Fragment}?"
+ string.Join("&", new RouteValueDictionary(uac.Values).Select(p => p.Key + "=" + p.Value)));
controller.Url = urlHelperMock.Object;
To my generic Controller setup. Which is a bit roughnready but means I can test any controller logic that generates links.

Owin.TestServer To Test a Middleware that Requires Authentication

I have a middleware method that requires context.Authentication.User.Identity.Name to be resolved for proper execution. However, when writing a unit test these properties are obviously null as no sign-in has occurred. I am not using Oauth or anything authentication related in this middleware (beyond the obvious name property), as it should be handled elsewhere in another middleware (to promote re-use/flexibility of the component I am developing). Is there a way to mock/fake this value so I can run my test? I have tried everything I can think of to fake a sign-on and I am just stuck at this point. To be clear the middleware needs the value not a webapi call or the like.
//Arrange
var resolver = A.Fake<IDependencyResolver>();
A.CallTo(() => resolver.GetService(typeof(ISomeService))).Returns(new TestService());
using (var server = TestServer.Create(app =>
{
app.UseMyMiddleware(new MyMiddlewareOptions()
{
DependencyResolver = resolver
});
app.Run(async ctx =>
{
await ctx.Response.WriteAsync(ctx.Request.Path.Value);
});
}))
{
//Act
var response = await server.CreateRequest("/").GetAsync();
//Assert
A.CallTo(() => resolver.GetService(typeof(ISomeService)))
.MustHaveHappened(Repeated.Exactly.Once);
Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
//Etc.
}
So here is one way I suppose not thrilled with it but it does the job. I will wait to accept as I imagine there should be a better way.
public class TestFakeLoginMiddleware : OwinMiddleware
{
public TestFakeLoginMiddleware(OwinMiddleware next) : base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
var identity = A.Fake<IIdentity>();
A.CallTo(() => identity.Name).Returns("TEST#domain.local");
var user = new ClaimsPrincipal(identity);
context.Request.Context.Authentication.User = user;
await Next.Invoke(context);
}
}
A bit late, I know, but could you not just create a new ClaimsIdentity?
public override async Task Invoke(IOwinContext context)
{
var identity= new ClaimsIdentity(new List<Claim> {
new Claim(ClaimTypes.Name, "TEST#domain.local")
});
var user = new ClaimsPrincipal(identity);
context.Request.Context.Authentication.User = user;
await Next.Invoke(context);
}