import { Matchers, Pact, SpecificationVersion } from '@pact-foundation/pact';
import { TestBed } from '@angular/core/testing';
-import { provideHttpClient } from '@angular/common/http';
+import { HttpErrorResponse, provideHttpClient } from '@angular/common/http';
import { lastValueFrom } from 'rxjs';
import { ChatroomService } from './chatroom.service';
import { Chatroom } from './chatroom.model';
expect(chatrooms).toContainEqual(EXAMPLE_CHATROOM);
});
});
+
+ it('a request for /list returns an HTTP 200 and an empty list, if no chatrooms are known', async () => {
+
+ await provider
+ .addInteraction()
+ .given('no chatrooms are known')
+ .uponReceiving('a request for /list')
+ .withRequest('GET', '/list')
+ .willRespondWith(200, (builder) => {
+ builder.headers({ 'Content-Type': 'application/json' });
+ builder.jsonBody([]);
+ })
+ .executeTest(async (mockserver) => {
+ await TestBed.configureTestingModule({
+ providers: [
+ ChatroomService,
+ {provide: APP_PROPS, useValue: {backendUri: mockserver.url + '/'}},
+ provideHttpClient(),
+ ]
+ });
+
+ const service = TestBed.inject(ChatroomService);
+ const chatrooms = await lastValueFrom(service.getChatrooms());
+
+ expect(chatrooms).toHaveLength(0);
+ });
+ });
+
+ it('a request for /1 with header "X-Shard: 7" returns an HTTP 200 and the chatroom, if the chatroom with id "1" exists in shard 7', async () => {
+
+ await provider
+ .addInteraction()
+ .given('chatroom with ID "1" exists in shard "7"')
+ .uponReceiving('a request for /1')
+ .withRequest('GET', '/1', (builder) => {
+ builder.headers({ 'X-Shard': 7 })
+ })
+ .willRespondWith(200, (builder) => {
+ builder.headers({ 'Content-Type': 'application/json' });
+ builder.jsonBody(EXAMPLE_CHATROOM);
+ })
+ .executeTest(async (mockserver) => {
+ await TestBed.configureTestingModule({
+ providers: [
+ ChatroomService,
+ {provide: APP_PROPS, useValue: {backendUri: mockserver.url + '/'}},
+ provideHttpClient(),
+ ]
+ });
+
+ const service = TestBed.inject(ChatroomService);
+ const chatroom = await lastValueFrom(service.getChatroom("7", "1"));
+
+ expect(chatroom).toEqual(EXAMPLE_CHATROOM);
+ });
+ });
+
+ it('a request for /1 with header "X-Shard: 6" returns HTTP 404, if the chatroom with id "1" exists in shard 7', async () => {
+
+ await provider
+ .addInteraction()
+ .given('chatroom with ID "1" exists in shard "7"')
+ .uponReceiving('a request for /1')
+ .withRequest('GET', '/1', (builder) => {
+ builder.headers({ 'X-Shard': 6 })
+ })
+ .willRespondWith(404)
+ .executeTest(async (mockserver) => {
+ await TestBed.configureTestingModule({
+ providers: [
+ ChatroomService,
+ {provide: APP_PROPS, useValue: {backendUri: mockserver.url + '/'}},
+ provideHttpClient(),
+ ]
+ });
+
+ const service = TestBed.inject(ChatroomService);
+
+ return new Promise<void>((resolve, reject) => {
+
+ service
+ .getChatroom("6", "1")
+ .subscribe({
+ next: (chatrooms) => {
+ reject('a status other than 200 should have rejected the observable')
+ },
+ error: (error) => {
+ resolve();
+ },
+ });
+ });
+ });
+ });
});