@@ -2,17 +2,17 @@ package connections
22
33import (
44 "encoding/json"
5- "fmt"
65 "github.com/stretchr/testify/assert"
76 "net/http"
87 "net/http/httptest"
98 "testing"
109)
1110
12- // TestBaseApiCall - Test for the ApiCallSetup function
13- func TestBaseApiCall (t * testing.T ) {
11+ // TestApiCallSetup - Test for the ApiCallSetup function
12+ func TestApiCallSetup (t * testing.T ) {
1413 expectedData := map [string ]string {"key" : "value" }
1514
15+ // Create a test server
1616 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1717 w .WriteHeader (http .StatusOK )
1818 err := json .NewEncoder (w ).Encode (expectedData )
@@ -22,15 +22,13 @@ func TestBaseApiCall(t *testing.T) {
2222
2323 var target map [string ]string
2424
25- err := ApiCallSetup (ts .URL , & target )
26- if err != nil {
27- return
28- }
25+ // Call ApiCallSetup with skipHTTPSCheck set to true
26+ err := ApiCallSetup (ts .URL , & target , true )
27+ assert .Nil (t , err , "Expected no error for skipHTTPSCheck" )
2928
30- assert .Equal (t , expectedData , target )
29+ assert .Equal (t , expectedData , target , "Expected data does not match the response" )
3130}
3231
33- // TestPokemonApiCall - Test for the PokemonApiCall function
3432func TestPokemonApiCall (t * testing.T ) {
3533 expectedPokemon := PokemonJSONStruct {
3634 Name : "pikachu" ,
@@ -54,17 +52,17 @@ func TestPokemonApiCall(t *testing.T) {
5452 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
5553 w .WriteHeader (http .StatusOK )
5654 err := json .NewEncoder (w ).Encode (expectedPokemon )
57- assert .Nil (t , err )
55+ assert .Nil (t , err , "failed to encode mock response" )
5856 }))
5957 defer ts .Close ()
6058
6159 pokemon , name , id , weight , height := PokemonApiCall ("/pokemon" , "pikachu" , ts .URL )
6260
63- assert .Equal (t , expectedPokemon , pokemon )
64- assert .Equal (t , "pikachu" , name )
65- assert .Equal (t , 25 , id )
66- assert .Equal (t , 60 , weight )
67- assert .Equal (t , 4 , height )
61+ assert .Equal (t , expectedPokemon , pokemon , "Expected Pokémon struct does not match" )
62+ assert .Equal (t , "pikachu" , name , "Expected name does not match" )
63+ assert .Equal (t , 25 , id , "Expected ID does not match" )
64+ assert .Equal (t , 60 , weight , "Expected weight does not match" )
65+ assert .Equal (t , 4 , height , "Expected height does not match" )
6866}
6967
7068// TestTypesApiCall - Test for the TypesApiCall function
@@ -100,65 +98,3 @@ func TestTypesApiCall(t *testing.T) {
10098 assert .Equal (t , "electric" , name )
10199 assert .Equal (t , 13 , id )
102100}
103-
104- func TestApiCallSetup_NotFound (t * testing.T ) {
105- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
106- w .WriteHeader (http .StatusNotFound )
107- fmt .Println (w , `{"error": "not found"}` )
108- }))
109- defer ts .Close ()
110-
111- var target map [string ]string
112- err := ApiCallSetup (ts .URL , & target )
113- if err != nil {
114- return
115- }
116- // TODO: Add assertions for the output or error message handling
117- }
118-
119- func TestPokemonApiCall_UnmarshalError (t * testing.T ) {
120- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
121- w .WriteHeader (http .StatusOK )
122- fmt .Println (w , `{"name": "123", "id": "not_a_number"}` ) // Partially malformed JSON
123- }))
124- defer ts .Close ()
125-
126- var pokemonStruct PokemonJSONStruct
127- err := ApiCallSetup (ts .URL , & pokemonStruct )
128- assert .NotNil (t , err , "Expected unmarshalling error due to type mismatch" )
129-
130- var typesStruct TypesJSONStruct
131- err = ApiCallSetup (ts .URL , & typesStruct )
132- assert .NotNil (t , err , "Expected unmarshalling error due to type mismatch" )
133- }
134-
135- func TestTypesApiCall_SuccessWithAllFields (t * testing.T ) {
136- expectedTypes := TypesJSONStruct {
137- Name : "electric" ,
138- ID : 13 ,
139- // TODO: Add fields to test complex struct parsing like `DamageRelations`
140- }
141-
142- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
143- w .WriteHeader (http .StatusOK )
144- err := json .NewEncoder (w ).Encode (expectedTypes )
145- assert .Nil (t , err )
146- }))
147- defer ts .Close ()
148-
149- types , _ , _ := TypesApiCall ("/type" , "electric" , ts .URL )
150- assert .Equal (t , expectedTypes , types )
151- }
152-
153- func TestApiCallSetup_Handles404 (t * testing.T ) {
154- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
155- w .WriteHeader (http .StatusNotFound )
156- }))
157- defer ts .Close ()
158-
159- var target map [string ]string
160- err := ApiCallSetup (ts .URL , & target )
161-
162- assert .NotNil (t , err )
163- assert .Contains (t , err .Error (), "404 error" )
164- }
0 commit comments