53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package feriados
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFetchYear(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/feriados/cidade/3106200" || r.URL.Query().Get("ano") != "2026" {
|
|
t.Errorf("requisição inesperada: %s", r.URL)
|
|
}
|
|
if r.Header.Get("Authorization") != "Bearer chave" {
|
|
t.Errorf("Authorization = %q", r.Header.Get("Authorization"))
|
|
}
|
|
_, _ = w.Write([]byte(`{"feriados":[
|
|
{"data":"01/01/2026","nome":"Ano Novo","tipo":"NACIONAL"},
|
|
{"data":"15/08/2026","nome":"Assunção de Nossa Senhora","tipo":"MUNICIPAL"},
|
|
{"data":"31/12/2025","nome":"Outro ano"},
|
|
{"data":"lixo","nome":"Inválido"}]}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
got, err := NewClient(srv.URL, "chave", "3106200").FetchYear(context.Background(), 2026)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
days := map[string]string{}
|
|
for _, h := range got {
|
|
days[h.Day] = h.Name
|
|
}
|
|
if len(days) != 2 || days["2026-01-01"] != "Ano Novo" || days["2026-08-15"] != "Assunção de Nossa Senhora" {
|
|
t.Fatalf("feriados = %v", days)
|
|
}
|
|
}
|
|
|
|
func TestFetchYearErrors(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if _, err := NewClient(srv.URL, "chave", "3106200").FetchYear(context.Background(), 2026); err == nil || !strings.Contains(err.Error(), "401") {
|
|
t.Fatalf("esperava HTTP 401, veio %v", err)
|
|
}
|
|
if _, err := NewClient(srv.URL, "chave", "MG-belo-horizonte").FetchYear(context.Background(), 2026); err == nil || !strings.Contains(err.Error(), "IBGE") {
|
|
t.Fatalf("esperava erro de código IBGE, veio %v", err)
|
|
}
|
|
}
|