-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
response combiner register added to the proxy register
- Loading branch information
Showing
4 changed files
with
140 additions
and
19 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
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
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,48 @@ | ||
package proxy | ||
|
||
import ( | ||
"github.com/devopsfaith/krakend/register" | ||
) | ||
|
||
type ResponseCombinerRegister interface { | ||
GetResponseCombiner(string) (ResponseCombiner, bool) | ||
SetResponseCombiner(string, ResponseCombiner) | ||
} | ||
|
||
func NewRegister() *Register { | ||
return &Register{ | ||
responseCombiners, | ||
} | ||
} | ||
|
||
type Register struct { | ||
*combinerRegister | ||
} | ||
|
||
type combinerRegister struct { | ||
data register.Untyped | ||
fallback ResponseCombiner | ||
} | ||
|
||
func newCombinerRegister(data map[string]ResponseCombiner, fallback ResponseCombiner) *combinerRegister { | ||
r := register.NewUntyped() | ||
for k, v := range data { | ||
r.Register(k, v) | ||
} | ||
return &combinerRegister{r, fallback} | ||
} | ||
|
||
func (r *combinerRegister) GetResponseCombiner(name string) (ResponseCombiner, bool) { | ||
v, ok := r.data.Get(name) | ||
if !ok { | ||
return r.fallback, ok | ||
} | ||
if rc, ok := v.(ResponseCombiner); ok { | ||
return rc, ok | ||
} | ||
return r.fallback, ok | ||
} | ||
|
||
func (r *combinerRegister) SetResponseCombiner(name string, rc ResponseCombiner) { | ||
r.data.Register(name, rc) | ||
} |
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,79 @@ | ||
package proxy | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestNewRegister_responseCombiner_ok(t *testing.T) { | ||
r := NewRegister() | ||
r.SetResponseCombiner("name1", func(_ context.Context, total int, parts []*Response) *Response { | ||
if total < 0 || total >= len(parts) { | ||
return nil | ||
} | ||
return parts[total] | ||
}) | ||
|
||
rc, ok := r.GetResponseCombiner("name1") | ||
if !ok { | ||
t.Error("expecting response combiner") | ||
return | ||
} | ||
|
||
result := rc(context.Background(), 0, []*Response{{IsComplete: true, Data: map[string]interface{}{"a": 42}}}) | ||
|
||
if result == nil { | ||
t.Error("expecting result") | ||
return | ||
} | ||
|
||
if !result.IsComplete { | ||
t.Error("expecting a complete result") | ||
return | ||
} | ||
|
||
if len(result.Data) != 1 { | ||
t.Error("unexpected result size:", len(result.Data)) | ||
return | ||
} | ||
} | ||
|
||
func TestNewRegister_responseCombiner_fallbackIfErrored(t *testing.T) { | ||
r := NewRegister() | ||
|
||
r.data.Register("errored", true) | ||
|
||
rc, ok := r.GetResponseCombiner("errored") | ||
if !ok { | ||
t.Error("expecting response combiner") | ||
return | ||
} | ||
|
||
original := &Response{IsComplete: true, Data: map[string]interface{}{"a": 42}} | ||
|
||
result := rc(context.Background(), 0, []*Response{original}) | ||
|
||
if result != original { | ||
t.Error("unexpected result:", result) | ||
return | ||
} | ||
} | ||
|
||
func TestNewRegister_responseCombiner_fallbackIfUnknown(t *testing.T) { | ||
r := NewRegister() | ||
|
||
rc, ok := r.GetResponseCombiner("unkown") | ||
if ok { | ||
t.Error("the response combiner should not be found") | ||
return | ||
} | ||
|
||
original := &Response{IsComplete: true, Data: map[string]interface{}{"a": 42}} | ||
|
||
result := rc(context.Background(), 0, []*Response{original}) | ||
|
||
if result != original { | ||
t.Error("unexpected result:", result) | ||
return | ||
} | ||
} |