show response time with status as stderr
parent
1546b88225
commit
e2b17a261f
39
main.go
39
main.go
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -23,8 +24,16 @@ type LagReader struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
err := Main()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Main() error {
|
||||||
var bodyRepeat int
|
var bodyRepeat int
|
||||||
var path, host, method, body, headers, brandID, issuer, basicAuth, claims string
|
var path, host, method, body, headers, brandID, userID, issuer, basicAuth, claims string
|
||||||
var ca, cert, key, secret string
|
var ca, cert, key, secret string
|
||||||
var needJWT, verbose, jsonPP bool
|
var needJWT, verbose, jsonPP bool
|
||||||
var timeout, lag time.Duration
|
var timeout, lag time.Duration
|
||||||
|
|
@ -34,6 +43,7 @@ func main() {
|
||||||
flag.StringVar(&body, "body", "", "body for request")
|
flag.StringVar(&body, "body", "", "body for request")
|
||||||
flag.IntVar(&bodyRepeat, "bodyrepeat", 1, "repeat body for request")
|
flag.IntVar(&bodyRepeat, "bodyrepeat", 1, "repeat body for request")
|
||||||
flag.StringVar(&brandID, "brand", "testencresponse", "brandID for request JWT")
|
flag.StringVar(&brandID, "brand", "testencresponse", "brandID for request JWT")
|
||||||
|
flag.StringVar(&userID, "user", "breel", "userid for request JWT")
|
||||||
flag.StringVar(&basicAuth, "auth", "", "comma separated user,password for basic auth")
|
flag.StringVar(&basicAuth, "auth", "", "comma separated user,password for basic auth")
|
||||||
flag.StringVar(&headers, "headers", "", "headers as k=v,k=v for request")
|
flag.StringVar(&headers, "headers", "", "headers as k=v,k=v for request")
|
||||||
flag.StringVar(&issuer, "issuer", "dataprocessing,responseengine,fieldset-definitions,qualtrics,objectstore,svs,monolith,ex,blixt,null,responseengine", "issuer for jwt")
|
flag.StringVar(&issuer, "issuer", "dataprocessing,responseengine,fieldset-definitions,qualtrics,objectstore,svs,monolith,ex,blixt,null,responseengine", "issuer for jwt")
|
||||||
|
|
@ -67,8 +77,9 @@ func main() {
|
||||||
reqBody,
|
reqBody,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
req.Header.Add("brandId", brandID)
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
if len(headers) > 0 {
|
if len(headers) > 0 {
|
||||||
for _, pair := range strings.Split(headers, ",") {
|
for _, pair := range strings.Split(headers, ",") {
|
||||||
|
|
@ -77,7 +88,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if needJWT {
|
if needJWT {
|
||||||
setJWT(req, brandID, issuer, secret, claims)
|
setJWT(verbose, req, brandID, userID, issuer, secret, claims)
|
||||||
}
|
}
|
||||||
if basicAuth != "" {
|
if basicAuth != "" {
|
||||||
splits := strings.Split(basicAuth, ",")
|
splits := strings.Split(basicAuth, ",")
|
||||||
|
|
@ -87,22 +98,22 @@ func main() {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Fprintf(os.Stderr, "%v\n", req)
|
fmt.Fprintf(os.Stderr, "%v\n", req)
|
||||||
}
|
}
|
||||||
|
start := time.Now()
|
||||||
resp, err := c.Do(req)
|
resp, err := c.Do(req)
|
||||||
|
elapsed := time.Since(start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("DO FAILED:", err)
|
return fmt.Errorf("DO failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Fprintf(os.Stderr, "%v\n", resp.Header)
|
fmt.Fprintf(os.Stderr, "%v\n", resp.Header)
|
||||||
}
|
}
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("READ BODY FAILED:", err)
|
return fmt.Errorf("READ BODY failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "(%d) ", resp.StatusCode)
|
fmt.Fprintf(os.Stderr, "(%d / %v) ", resp.StatusCode, elapsed)
|
||||||
if jsonPP {
|
if jsonPP {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
if err := json.Unmarshal(b, &v); err == nil {
|
if err := json.Unmarshal(b, &v); err == nil {
|
||||||
|
|
@ -112,6 +123,10 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", bytes.TrimSpace(b))
|
fmt.Printf("%s\n", bytes.TrimSpace(b))
|
||||||
|
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode != http.StatusNotFound {
|
||||||
|
return fmt.Errorf("Status %v", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeClient(timeout time.Duration, ca, cert, key string) *http.Client {
|
func makeClient(timeout time.Duration, ca, cert, key string) *http.Client {
|
||||||
|
|
@ -143,13 +158,13 @@ func makeClient(timeout time.Duration, ca, cert, key string) *http.Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setJWT(r *http.Request, brandID string, issuer, secret, claims string) {
|
func setJWT(verbose bool, r *http.Request, brandID, userID string, issuer, secret, claims string) {
|
||||||
signer := &jwt.Signer{
|
signer := &jwt.Signer{
|
||||||
Key: []byte(secret),
|
Key: []byte(secret),
|
||||||
DefaultClaims: jwt.Claims{
|
DefaultClaims: jwt.Claims{
|
||||||
Audience: "qualtrics",
|
Audience: "qualtrics",
|
||||||
Issuer: issuer,
|
Issuer: issuer,
|
||||||
UserID: "breel",
|
UserID: userID,
|
||||||
BrandID: brandID,
|
BrandID: brandID,
|
||||||
Custom: map[string]interface{}{
|
Custom: map[string]interface{}{
|
||||||
"IsolationPartitionID": brandID,
|
"IsolationPartitionID": brandID,
|
||||||
|
|
@ -169,6 +184,10 @@ func setJWT(r *http.Request, brandID string, issuer, secret, claims string) {
|
||||||
if err := signer.Sign(r, jwt.Claims{}); err != nil {
|
if err := signer.Sign(r, jwt.Claims{}); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if verbose {
|
||||||
|
log.Println(*signer)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLagReader(lag time.Duration, src io.Reader) *LagReader {
|
func NewLagReader(lag time.Duration, src io.Reader) *LagReader {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue