-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from eliseeviam/basedOnRequest-strategy
new: basedOnRequest strategy implemented
- Loading branch information
Showing
10 changed files
with
311 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
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,30 @@ | ||
- name: Test concurrent with query mathing | ||
mocks: | ||
backend: | ||
strategy: basedOnRequest | ||
uris: | ||
- strategy: constant | ||
body: > | ||
{ | ||
"value": 1 | ||
} | ||
requestConstraints: | ||
- kind: queryMatches | ||
expectedQuery: "key=value1" | ||
- kind: pathMatches | ||
path: /request | ||
- strategy: constant | ||
body: > | ||
{ | ||
"value": 22 | ||
} | ||
requestConstraints: | ||
- kind: queryMatches | ||
expectedQuery: "key=value2" | ||
- kind: pathMatches | ||
path: /request | ||
method: GET | ||
path: /do | ||
response: | ||
200: | | ||
{"total":23} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/lamoda/gonkey/mocks" | ||
"github.com/lamoda/gonkey/runner" | ||
) | ||
|
||
func TestProxy(t *testing.T) { | ||
m := mocks.NewNop("backend") | ||
if err := m.Start(); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer m.Shutdown() | ||
|
||
os.Setenv("BACKEND_ADDR", m.Service("backend").ServerAddr()) | ||
initServer() | ||
srv := httptest.NewServer(nil) | ||
|
||
runner.RunWithTesting(t, &runner.RunWithTestingParams{ | ||
Server: srv, | ||
TestsDir: "cases", | ||
Mocks: m, | ||
}) | ||
} |
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,71 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"golang.org/x/sync/errgroup" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
urlpkg "net/url" | ||
"os" | ||
"sync/atomic" | ||
) | ||
|
||
func main() { | ||
initServer() | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func initServer() { | ||
http.HandleFunc("/do", Do) | ||
} | ||
|
||
func Do(w http.ResponseWriter, r *http.Request) { | ||
params1 := urlpkg.Values{"key": []string{"value1"}}.Encode() | ||
params2 := urlpkg.Values{"key": []string{"value2"}}.Encode() | ||
doRequest := func(params string) (int64, error) { | ||
url := fmt.Sprintf("http://%s/request?%s", os.Getenv("BACKEND_ADDR"), params) | ||
res, err := http.Get(url) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
return 0, fmt.Errorf("backend response status code %d", res.StatusCode) | ||
} | ||
body, err := ioutil.ReadAll(res.Body) | ||
_ = res.Body.Close() | ||
if err != nil { | ||
return 0, fmt.Errorf("cannot read response body %w", err) | ||
} | ||
var resp struct { | ||
Value int64 `json:"value"` | ||
} | ||
err = json.Unmarshal(body, &resp) | ||
if err != nil { | ||
return 0, fmt.Errorf("cannot unmarshal response body %w", err) | ||
} | ||
return resp.Value, nil | ||
} | ||
|
||
var total int64 | ||
errg := errgroup.Group{} | ||
errg.Go(func() error { | ||
v, err := doRequest(params1) | ||
atomic.AddInt64(&total, v) | ||
return err | ||
}) | ||
errg.Go(func() error { | ||
v, err := doRequest(params2) | ||
atomic.AddInt64(&total, v) | ||
return err | ||
}) | ||
err := errg.Wait() | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
_, _ = io.WriteString(w, err.Error()) | ||
return | ||
} | ||
_, _ = fmt.Fprintf(w, `{"total":%v}`, total) | ||
} |
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
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