jec-sandcat
Version:
JEC Sandcat - The default RESTful web services framework for GlassCat applications.
152 lines (138 loc) • 5.36 kB
text/typescript
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
//
// Copyright 2016-2018 Pascal ECHEMANN.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { TestSuite, Test, BeforeAll, TestSorters } from "jec-juta";
import { expect } from "chai";
import { UrlPatternMapper } from "../../../../../src/com/onsoft/sandcat/core/UrlPatternMapper";
import { UrlPatternMatcher } from "../../../../../src/com/onsoft/sandcat/core/UrlPatternMatcher";
import { RouteDescriptor } from "../../../../../src/com/onsoft/sandcat/reflect/RouteDescriptor";
import { RequestProperties } from "../../../../../src/com/onsoft/sandcat/utils/RequestProperties";
import { HttpMethod } from "jec-commons";
export class UrlPatternMapperTest {
public mapper:UrlPatternMapper = null;
public descriptor1:RouteDescriptor = null;
public descriptor2:RouteDescriptor = null;
public initTest():void {
this.mapper = new UrlPatternMapper();
this.descriptor1 = new RouteDescriptor(
"/api/users/:id",
"getUser",
HttpMethod.GET
);
this.descriptor2 = new RouteDescriptor(
"/api/search",
"doSearch",
HttpMethod.GET
);
}
public hasRegisteredMethodTest():void {
expect(this.mapper.hasRegisteredMethod(HttpMethod.GET)).to.be.false;
}
public addRouteDescriptorTest():void {
expect(this.mapper.addRouteDescriptor(this.descriptor1)).to.be.undefined;
}
public hasRegisteredMethodTrueTest():void {
expect(this.mapper.hasRegisteredMethod(HttpMethod.GET)).to.be.true;
}
public addRouteDescriptorSameMethodTest():void {
expect(this.mapper.addRouteDescriptor(this.descriptor2)).to.be.undefined;
}
public matchRequestFailTest():void {
const props:RequestProperties = new RequestProperties();
props.subRoute = "/subroute";
expect(this.mapper.matchRequest(props)).to.be.null;
}
public matchRequestTest():void {
const props:RequestProperties = new RequestProperties();
props.subRoute = "/api/search";
props.httpMethod = HttpMethod.GET;
expect(
this.mapper.matchRequest(props)
).to.be.an.instanceOf(UrlPatternMatcher);
}
public matchRequestQueryTest():void {
const props:RequestProperties = new RequestProperties();
props.subRoute = "/api/search?name=foo&id=bar";
props.httpMethod = HttpMethod.GET;
const matcher:UrlPatternMatcher = this.mapper.matchRequest(props);
expect(matcher.queryParams.name).to.equal("foo");
expect(matcher.queryParams.id).to.equal("bar");
}
public matchDescriptorTest():void {
const props:RequestProperties = new RequestProperties();
props.subRoute = "/api/users/30";
props.httpMethod = HttpMethod.GET;
const matcher:UrlPatternMatcher = this.mapper.matchRequest(props);
expect(matcher.descriptor).to.be.equal(this.descriptor1);
}
public matchPropertiesTest():void {
const props:RequestProperties = new RequestProperties();
props.subRoute = "/api/users/30";
props.httpMethod = HttpMethod.GET;
const matcher:UrlPatternMatcher = this.mapper.matchRequest(props);
expect(matcher.properties.id).to.be.equal("30");
}
public matchKoTest():void {
const props:RequestProperties = new RequestProperties();
props.subRoute = "/api/users/30/data";
props.httpMethod = HttpMethod.GET;
expect(this.mapper.matchRequest(props)).to.be.null;
}
}