show response time with status as stderr

master
Bel LaPointe 2019-09-23 10:03:28 -06:00
parent 1546b88225
commit e2b17a261f
1 changed files with 29 additions and 10 deletions

39
main.go
View File

@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
@ -23,8 +24,16 @@ type LagReader struct {
}
func main() {
err := Main()
if err != nil {
log.Println(err)
os.Exit(1)
}
}
func Main() error {
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 needJWT, verbose, jsonPP bool
var timeout, lag time.Duration
@ -34,6 +43,7 @@ func main() {
flag.StringVar(&body, "body", "", "body for request")
flag.IntVar(&bodyRepeat, "bodyrepeat", 1, "repeat body for request")
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(&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")
@ -67,8 +77,9 @@ func main() {
reqBody,
)
if err != nil {
panic(err)
return err
}
req.Header.Add("brandId", brandID)
req.Header.Add("Content-Type", "application/json")
if len(headers) > 0 {
for _, pair := range strings.Split(headers, ",") {
@ -77,7 +88,7 @@ func main() {
}
}
if needJWT {
setJWT(req, brandID, issuer, secret, claims)
setJWT(verbose, req, brandID, userID, issuer, secret, claims)
}
if basicAuth != "" {
splits := strings.Split(basicAuth, ",")
@ -87,22 +98,22 @@ func main() {
if verbose {
fmt.Fprintf(os.Stderr, "%v\n", req)
}
start := time.Now()
resp, err := c.Do(req)
elapsed := time.Since(start)
if err != nil {
fmt.Println("DO FAILED:", err)
return
return fmt.Errorf("DO failed: %v", err)
}
if verbose {
fmt.Fprintf(os.Stderr, "%v\n", resp.Header)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("READ BODY FAILED:", err)
return
return fmt.Errorf("READ BODY failed: %v", err)
}
defer resp.Body.Close()
fmt.Fprintf(os.Stderr, "(%d) ", resp.StatusCode)
fmt.Fprintf(os.Stderr, "(%d / %v) ", resp.StatusCode, elapsed)
if jsonPP {
var v interface{}
if err := json.Unmarshal(b, &v); err == nil {
@ -112,6 +123,10 @@ func main() {
}
}
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 {
@ -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{
Key: []byte(secret),
DefaultClaims: jwt.Claims{
Audience: "qualtrics",
Issuer: issuer,
UserID: "breel",
UserID: userID,
BrandID: brandID,
Custom: map[string]interface{}{
"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 {
panic(err)
}
if verbose {
log.Println(*signer)
}
}
func NewLagReader(lag time.Duration, src io.Reader) *LagReader {