mirror of
https://github.com/GNS3/gns3-web-ui.git
synced 2025-05-31 22:40:43 +00:00
Tests for HttpServer
This commit is contained in:
parent
8dcace80e0
commit
b4e2b38585
@ -1,15 +1,115 @@
|
|||||||
import { TestBed, inject } from '@angular/core/testing';
|
import { TestBed, } from '@angular/core/testing';
|
||||||
|
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
||||||
|
import { HttpClient } from "@angular/common/http";
|
||||||
|
|
||||||
|
import { Server } from '../models/server';
|
||||||
import { HttpServer } from './http-server.service';
|
import { HttpServer } from './http-server.service';
|
||||||
|
|
||||||
|
|
||||||
describe('HttpServer', () => {
|
describe('HttpServer', () => {
|
||||||
|
let httpClient: HttpClient;
|
||||||
|
let httpTestingController: HttpTestingController;
|
||||||
|
let service: HttpServer;
|
||||||
|
let server: Server;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
providers: [HttpServer]
|
imports: [
|
||||||
});
|
HttpClientTestingModule
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
HttpServer
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
// it('should be created', inject([HttpServer], (service: HttpServer) => {
|
httpClient = TestBed.get(HttpClient);
|
||||||
// expect(service).toBeTruthy();
|
httpTestingController = TestBed.get(HttpTestingController);
|
||||||
// }));
|
service = TestBed.get(HttpServer);
|
||||||
|
|
||||||
|
server = new Server();
|
||||||
|
server.ip = "127.0.0.1";
|
||||||
|
server.port = 3080;
|
||||||
|
server.authorization = "none";
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
httpTestingController.verify();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make GET query for get method', () => {
|
||||||
|
service.get(server, '/test').subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("GET");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make GET query for getText method', () => {
|
||||||
|
service.getText(server, '/test').subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("GET");
|
||||||
|
expect(req.request.responseType).toEqual("text");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make POST query for post method', () => {
|
||||||
|
service.post(server, '/test', {test: "1"}).subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("POST");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make PUT query for put method', () => {
|
||||||
|
service.put(server, '/test', {test: "1"}).subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("PUT");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make DELETE query for delete method', () => {
|
||||||
|
service.delete(server, '/test').subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("DELETE");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make PATCH query for patch method', () => {
|
||||||
|
service.patch(server, '/test', {test: "1"}).subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("PATCH");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make HEAD query for head method', () => {
|
||||||
|
service.head(server, '/test').subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("HEAD");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make OPTIONS query for options method', () => {
|
||||||
|
service.options(server, '/test').subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("OPTIONS");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add headers for `basic` authorization', () => {
|
||||||
|
server.authorization = "basic";
|
||||||
|
server.login = "login";
|
||||||
|
server.password = "password";
|
||||||
|
|
||||||
|
service.get(server, '/test').subscribe();
|
||||||
|
|
||||||
|
const req = httpTestingController.expectOne('http://127.0.0.1:3080/v2/test');
|
||||||
|
expect(req.request.method).toEqual("GET");
|
||||||
|
expect(req.request.responseType).toEqual("json");
|
||||||
|
expect(req.request.headers.get('Authorization')).toEqual('Basic bG9naW46cGFzc3dvcmQ=');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -114,8 +114,8 @@ export class HttpServer {
|
|||||||
private getOptionsForServer<T extends HeadersOptions>(server: Server, url: string, options: T) {
|
private getOptionsForServer<T extends HeadersOptions>(server: Server, url: string, options: T) {
|
||||||
url = `http://${server.ip}:${server.port}/v2${url}`;
|
url = `http://${server.ip}:${server.port}/v2${url}`;
|
||||||
|
|
||||||
if (options.headers === null) {
|
if (!options.headers) {
|
||||||
options.headers = new HttpHeaders();
|
options.headers = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (server.authorization === "basic") {
|
if (server.authorization === "basic") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user