]> juplo.de Git - demos/kafka/chat/commitdiff
test: Formulated more expections for the pact
authorKai Moritz <kai@juplo.de>
Sat, 11 Oct 2025 12:37:50 +0000 (14:37 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 11 Oct 2025 12:57:33 +0000 (14:57 +0200)
src/app/chatroom/chatroom.service.pact.spec.ts

index 38838f188dea3b35be1a6d88d775d28f7c07a5ea..44a59481eb864c3ef22b8d0b32ac3e8e8e3fb467 100644 (file)
@@ -1,6 +1,6 @@
 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';
@@ -51,4 +51,97 @@ describe('Pact between the ChatroomService and the backend', () => {
         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();
+              },
+            });
+        });
+      });
+  });
 });