Skip to content

Commit

Permalink
chore: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan committed Jan 16, 2019
1 parent d7f70a9 commit 249438e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 15 deletions.
97 changes: 97 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func TestIndexHandler(t *testing.T) {
var (
httpClient = initClient()
notifier = NewNotifier(*httpClient)
appConfig = &App{notifier}
)

// Create a request to pass to our handler.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(Handler{appConfig, handleIndex})

// handler satisfy http.Handler since ServeHTTP is implemented
handler.ServeHTTP(rr, req)

// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}

func TestHealthCheckHandler(t *testing.T) {
var (
httpClient = initClient()
notifier = NewNotifier(*httpClient)
appConfig = &App{notifier}
)

// Create a request to pass to our handler.
req, err := http.NewRequest("GET", "/ping", nil)
if err != nil {
t.Fatal(err)
}

// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(Handler{appConfig, handleHealthCheck})

// handler satisfy http.Handler since ServeHTTP is implemented
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}

func TestNewAlertHandler(t *testing.T) {
var (
httpClient = initClient()
notifier = NewNotifier(*httpClient)
appConfig = &App{notifier}
)

// Create a request to pass to our handler.
// Load mock payload
configFile, err := ioutil.ReadFile("examples/mock_payload.json")
if err != nil {
log.Fatal("Error opening config file", err.Error())
os.Exit(1)
}

req, err := http.NewRequest("POST", "/create?room_name=alertManagerTestRoom", bytes.NewBuffer(configFile))
if err != nil {
t.Fatal(err)
}

// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(Handler{appConfig, handleNewAlert})

// handler satisfy http.Handler since ServeHTTP is implemented
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
15 changes: 0 additions & 15 deletions main_test.go

This file was deleted.

0 comments on commit 249438e

Please sign in to comment.