-
Notifications
You must be signed in to change notification settings - Fork 0
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
78 additions
and
20 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 |
---|---|---|
@@ -1,31 +1,89 @@ | ||
package result | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewResult(t *testing.T) { | ||
t.Run("valid inputs", func(t *testing.T) { | ||
t.Fatalf("implement the test") | ||
}) | ||
|
||
t.Run("json content type", func(t *testing.T) { | ||
t.Fatalf("implement the test") | ||
}) | ||
func TestNewResultValidInputs(t *testing.T) { | ||
result, err := NewResult(100*time.Millisecond, "/path", "200 OK", 200, nil, "HTTP/2.0", nil) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if result.Path != "/path" { | ||
t.Errorf("expected path '/path', got '%v'", result.Path) | ||
} | ||
if result.Status != "200 OK" { | ||
t.Errorf("expected status '200 OK', got '%v'", result.Status) | ||
} | ||
if result.StatusCode != 200 { | ||
t.Errorf("expected status code '200', got '%v'", result.StatusCode) | ||
} | ||
if result.Protocol != "HTTP/2.0" { | ||
t.Errorf("expected protocol 'HTTP/2.0', got '%v'", result.Protocol) | ||
} | ||
if result.Body != nil { | ||
t.Errorf("expected body to be nil, got %v", result.Body) | ||
} | ||
if result.Text != "" { | ||
t.Errorf(`expected text to be empty , got '%v'`, result.Text) | ||
} | ||
} | ||
|
||
t.Run("text content type", func(t *testing.T) { | ||
t.Fatalf("implement the test") | ||
}) | ||
func TestNewResultJsonContent(t *testing.T) { | ||
headers := http.Header{} | ||
headers.Set("content-type", "application/json") | ||
bodyRaw := []byte(`{"key":"value"}`) | ||
result, err := NewResult(100*time.Millisecond, "/path", "200 OK", 200, headers, "HTTP/2.0", bodyRaw) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if result.Body == nil || result.Body["key"] != "value" { | ||
t.Errorf("expected body with key 'key' and value 'value', got %v", result.Body) | ||
} | ||
if result.Text != "" { | ||
t.Errorf(`expected text to be empty , got '%v'`, result.Text) | ||
} | ||
} | ||
|
||
t.Run("empty body", func(t *testing.T) { | ||
t.Fatalf("implement the test") | ||
}) | ||
func TestNewResultTextContent(t *testing.T) { | ||
headers := http.Header{} | ||
headers.Set("content-type", "text/plain") | ||
bodyRaw := []byte("plain text content") | ||
result, err := NewResult(100*time.Millisecond, "/path", "200 OK", 200, headers, "HTTP/2.0", bodyRaw) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if result.Body != nil { | ||
t.Errorf("expected body to be nil, got %v", result.Body) | ||
} | ||
if result.Text != "plain text content" { | ||
t.Errorf(`expected text to be 'plain text content' , got '%v'`, result.Text) | ||
} | ||
} | ||
|
||
t.Run("invalid json data", func(t *testing.T) { | ||
t.Fatalf("implement the test") | ||
}) | ||
func TestNewResultEmptyBody(t *testing.T) { | ||
headers := http.Header{} | ||
headers.Set("content-type", "application/json") | ||
result, err := NewResult(100*time.Millisecond, "/path", "200 OK", 200, headers, "HTTP/2.0", nil) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if result.Body != nil { | ||
t.Errorf("expected body to be nil, got %v", result.Body) | ||
} | ||
if result.Text != "" { | ||
t.Errorf(`expected text to be empty , got '%v'`, result.Text) | ||
} | ||
} | ||
|
||
t.Run("not json and not text content type", func(t *testing.T) { | ||
t.Fatalf("implement the test") | ||
}) | ||
func TestNewResultInvalidJson(t *testing.T) { | ||
headers := http.Header{} | ||
headers.Set("content-type", "application/json") | ||
bodyRaw := []byte(`{invalid json}`) | ||
_, err := NewResult(100*time.Millisecond, "/path", "200 OK", 200, headers, "HTTP/2.0", bodyRaw) | ||
if err == nil { | ||
t.Fatal("expected error, got nil") | ||
} | ||
} |