-
Notifications
You must be signed in to change notification settings - Fork 2
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
2 changed files
with
56 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package jrpc | ||
|
||
import "time" | ||
|
||
type jrpcOpt struct { | ||
CustomHeadersKey []string | ||
ReadHeaderTimeout time.Duration | ||
} | ||
|
||
type Option func(*jrpcOpt) | ||
|
||
func defaultOpt() *jrpcOpt { | ||
return &jrpcOpt{ | ||
ReadHeaderTimeout: 3 * time.Second, | ||
CustomHeadersKey: []string{"Authorization"}, | ||
} | ||
} | ||
|
||
// WithCustomHeaders add custom headers to metadata.MD | ||
func WithCustomHeaders(headersKey ...string) Option { | ||
return func(opt *jrpcOpt) { | ||
opt.CustomHeadersKey = append(opt.CustomHeadersKey, headersKey...) | ||
} | ||
} | ||
|
||
// WithReadHeaderTimeout set custom read header timeout | ||
func WithReadHeaderTimeout(timeout time.Duration) Option { | ||
return func(opt *jrpcOpt) { | ||
opt.ReadHeaderTimeout = timeout | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package jrpc | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestOptions(t *testing.T) { | ||
dur := 5 * time.Second | ||
|
||
opts := []Option{ | ||
WithCustomHeaders("foobar"), | ||
WithReadHeaderTimeout(dur), | ||
} | ||
|
||
def := defaultOpt() | ||
|
||
for _, opt := range opts { | ||
opt(def) | ||
} | ||
|
||
assert.Len(t, def.CustomHeadersKey, 2) | ||
assert.Equal(t, def.ReadHeaderTimeout.String(), dur.String()) | ||
} |