@kevinwatt/mcp-webhook
Version:
Generic Webhook MCP Server
65 lines (64 loc) • 2.34 kB
JavaScript
import { describe, expect, test, beforeEach, jest } from '@jest/globals';
import axios from 'axios';
// Mock axios
jest.mock('axios', () => ({
post: jest.fn()
}));
// Set environment variable
process.env.WEBHOOK_URL = 'https://example.com/webhook';
describe('Webhook Functionality', () => {
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
});
describe('send_message simulation', () => {
test('should post message with expected parameters', async () => {
// Setup
axios.post.mockResolvedValue({ data: 'success' });
// Exercise
const content = 'Test message';
const username = 'TestUser';
const avatar_url = 'https://example.com/avatar.png';
await axios.post('https://example.com/webhook', {
text: content,
username,
avatar_url
});
// Verify
expect(axios.post).toHaveBeenCalledWith('https://example.com/webhook', {
text: content,
username,
avatar_url
});
});
test('should handle axios errors', async () => {
// Setup
const mockError = new Error('Network error');
axios.post.mockRejectedValue(mockError);
// Exercise & Verify
await expect(async () => {
await axios.post('https://example.com/webhook', { text: 'Test message' });
}).rejects.toThrow('Network error');
});
});
describe('send_json simulation', () => {
test('should post JSON with expected structure', async () => {
// Setup
axios.post.mockResolvedValue({ data: 'success' });
// Exercise
const jsonBody = {
text: 'Hello from JSON',
attachments: [
{
color: '#36a64f',
title: 'Attachment Title',
text: 'Attachment text here'
}
]
};
await axios.post('https://example.com/webhook', jsonBody);
// Verify
expect(axios.post).toHaveBeenCalledWith('https://example.com/webhook', jsonBody);
});
});
});