Skip to content

Commit

Permalink
Merge pull request #25 from dami-pie/test/env
Browse files Browse the repository at this point in the history
test: cria teste unitário básico
  • Loading branch information
Sevlak committed Jul 6, 2023
2 parents 801141f + ba9f04b commit dfc01f7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Testa código do pull request
run-name: Testando a branch ${{ github.ref_name }}
run-name: Testando a branch ${{ github.base_ref }}

on: pull_request

Expand All @@ -19,4 +19,5 @@ jobs:
run: go mod download

- name: Testa
working-directory: .
run: go test -v ./...
44 changes: 44 additions & 0 deletions src/responses/responses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package responses

import (
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

type JSONTestCase struct {
name string
req *http.Request
want int
}

func TestDecodeJSON(t *testing.T) {
tests := []JSONTestCase{
{
name: "Content-Type inválido",
req: &http.Request{Header: http.Header{"Content-Type": []string{"application/xml"}}, Body: nil},
want: http.StatusUnsupportedMediaType,
},
{
name: "JSON mal formado",
req: &http.Request{Body: io.NopCloser(io.Reader(strings.NewReader("{\"email: \"aaaa\"}")))},
want: http.StatusBadRequest,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := DecodeJSON(&httptest.ResponseRecorder{}, test.req, "")

var mr *MalformedRequest
if errors.As(err, &mr) { //unwrap error
if test.want != mr.Status {
t.Errorf("Deveria receber %d, recebido foi %d", test.want, mr.Status)
}
}
})
}
}
20 changes: 10 additions & 10 deletions src/routes/routes_config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package routes

import "net/http"

type Route struct {
URI string
Metodo string
Funcao func(http.ResponseWriter, *http.Request)
RequerAutenticacao bool
}
package routes

import "net/http"

type Route struct {
URI string
Metodo string
Funcao func(http.ResponseWriter, *http.Request)
RequerAutenticacao bool
}

0 comments on commit dfc01f7

Please sign in to comment.