Skip to content

Commit

Permalink
Feature/more coverage (#7)
Browse files Browse the repository at this point in the history
* More coverage

* more coverage
  • Loading branch information
jlucaspains authored Sep 11, 2023
1 parent 7146cdf commit 06afb0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
7 changes: 1 addition & 6 deletions backend/handlers/certCheckHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ func (h Handlers) CheckStatus(w http.ResponseWriter, r *http.Request) {
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
resp, err := client.Get(params.Url)

if err != nil {
h.JSON(w, http.StatusBadRequest, &models.ErrorResult{Errors: []string{"Could not load provided URL"}})
return
}
resp, _ := client.Get(params.Url)

hostName := strings.Split(params.Url, ":")[1]
hostName = strings.Trim(hostName, "/")
Expand Down
9 changes: 9 additions & 0 deletions backend/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -36,6 +37,14 @@ func TestErrorTranslationSuccess(t *testing.T) {
assert.Equal(t, "Tagline2 should be greater than 1", result.Errors[0])
}

func TestErrorTranslationServerError(t *testing.T) {
handlers := new(Handlers)
code, result := handlers.ErrorToHttpResult(fmt.Errorf("Something went wrong"))
assert.Equal(t, http.StatusInternalServerError, code)

assert.Equal(t, "Unknown error", result.Errors[0])
}

func makeRequest[K any | []any](router *mux.Router, method string, url string, body any) (code int, respBody *K, err error) {
inputBody := ""

Expand Down
22 changes: 22 additions & 0 deletions backend/handlers/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package handlers

import (
"testing"

"github.com/gorilla/mux"
"github.com/jlucaspains/sharp-cert-checker/models"
"github.com/stretchr/testify/assert"
)

func TestGetHealth(t *testing.T) {
handlers := new(Handlers)

router := mux.NewRouter()
router.HandleFunc("/health", handlers.HealthCheck).Methods("GET")

code, body, err := makeRequest[models.HealthResult](router, "GET", "/health", nil)

assert.Nil(t, err)
assert.Equal(t, 200, code)
assert.True(t, body.Healthy)
}

0 comments on commit 06afb0e

Please sign in to comment.