-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,27 +96,50 @@ func TestSendJobRequest(t *testing.T) { | |
} | ||
} | ||
|
||
// Checks if the function returns a job ID and no error when it's called with a valid response. | ||
// Checks for a successful response, a response with a non-200 status code, and a response with invalid JSON. | ||
func TestHandleJobResponse(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.Write([]byte(`{"id": "12345"}`)) | ||
})) | ||
defer server.Close() | ||
|
||
client := server.Client() | ||
resp, _ := client.Get(server.URL) | ||
user := map[string]string{ | ||
"mail": "[email protected]", | ||
"username": "testuser", | ||
} | ||
|
||
// Test successful response | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.Write([]byte(`{"id": "12345"}`)) | ||
})) | ||
client := server.Client() | ||
resp, _ := client.Get(server.URL) | ||
jobId, err := handleJobResponse(resp, user) | ||
|
||
if err != nil { | ||
t.Errorf("handleJobResponse() returned an error: %v", err) | ||
} | ||
|
||
if jobId != "12345" { | ||
t.Errorf("handleJobResponse() returned job ID %v, want 12345", jobId) | ||
} | ||
server.Close() | ||
|
||
// Test non-200 status code | ||
server = httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
rw.Write([]byte(`{"id": "12345"}`)) | ||
})) | ||
client = server.Client() | ||
resp, _ = client.Get(server.URL) | ||
jobId, err = handleJobResponse(resp, user) | ||
if err == nil { | ||
t.Errorf("handleJobResponse() did not return an error for non-200 status code") | ||
} | ||
server.Close() | ||
|
||
// Test invalid JSON in response | ||
server = httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.Write([]byte(`invalid JSON`)) | ||
})) | ||
client = server.Client() | ||
resp, _ = client.Get(server.URL) | ||
jobId, err = handleJobResponse(resp, user) | ||
if err == nil { | ||
t.Errorf("handleJobResponse() did not return an error for invalid JSON") | ||
} | ||
server.Close() | ||
} |