Skip to content

Commit

Permalink
test: add test cases for result
Browse files Browse the repository at this point in the history
  • Loading branch information
eynopv committed Jun 16, 2024
1 parent 12d3d75 commit ccfbe5e
Showing 1 changed file with 78 additions and 20 deletions.
98 changes: 78 additions & 20 deletions pkg/result/result_test.go
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")
}
}

0 comments on commit ccfbe5e

Please sign in to comment.