Skip to content

Commit

Permalink
feat: accessor method for properties of a Result (closes #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
deltics committed Jul 4, 2024
1 parent 7901bad commit 8e9583d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
27 changes: 27 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ func (r Result) String() string {
r.statusCode, ifNil(r.contentType, "<nil>"), r.content, r.headers)
}

// Headers returns a copy of the headers set on the Result.
func (r *Result) Headers() headers {
h := make(headers, len(r.headers))
for k, v := range r.headers {
h[k] = v
}
return h
}

// StatusCode returns the status code set on the Result.
func (r *Result) StatusCode() int {
return r.statusCode
}

// Content returns the content set on the Result.
func (r *Result) Content() any {
return r.content
}

// ContentType returns the content type set on the Result.
func (r *Result) ContentType() string {
if r.contentType == nil {
return ""
}
return *r.contentType
}

// WithContent sets the content and content type of the Result. The
// specified content and content type will replace any content or
// content type that may have been set on the Result previously.
Expand Down
74 changes: 74 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,80 @@ func TestResult(t *testing.T) {
`}`)
},
},

// property accessors
{scenario: "Content",
exec: func(t *testing.T) {
// ARRANGE
sut := &Result{content: []byte("content")}

// ACT
result := sut.Content()

// ASSERT
test.That(t, result).Equals([]byte("content"))
},
},
{scenario: "ContentType",
exec: func(t *testing.T) {
// ARRANGE
sut := &Result{contentType: test.AddressOf("content/type")}

// ACT
result := sut.ContentType()

// ASSERT
test.That(t, result).Equals("content/type")
},
},
{scenario: "ContentType/nil",
exec: func(t *testing.T) {
// ARRANGE
sut := &Result{}

// ACT
result := sut.ContentType()

// ASSERT
test.That(t, result).Equals("")
},
},
{scenario: "Headers",
exec: func(t *testing.T) {
// ARRANGE
sut := &Result{headers: headers{
"Header": "value",
}}

// ACT
result := sut.Headers()

// ASSERT
test.Map(t, result).Equals(headers{
"Header": "value",
})

// ACT
result["Header"] = "new value"

// ASSERT
test.Map(t, sut.Headers(), "is a copy").Equals(headers{
"Header": "value",
})
},
},
{scenario: "StatusCode",
exec: func(t *testing.T) {
// ARRANGE
sut := &Result{statusCode: 404}

// ACT
result := sut.StatusCode()

// ASSERT
test.That(t, result).Equals(404)
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
Expand Down

0 comments on commit 8e9583d

Please sign in to comment.