Skip to content

Commit

Permalink
feat: add server custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Jun 18, 2024
1 parent 87e0220 commit 28fd084
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions jrpc/options.go
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
}
}
25 changes: 25 additions & 0 deletions jrpc/options_test.go
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())
}

0 comments on commit 28fd084

Please sign in to comment.