Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: add examples about ssl, debug, curl, trace, error, etc.
- Loading branch information
Showing
30 changed files
with
1,763 additions
and
9 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
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,19 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDIjCCAgqgAwIBAgIUPkbRM1znk+3fLI8g5eB5i8Ie3K0wDQYJKoZIhvcNAQEL | ||
BQAwFTETMBEGA1UEAwwKbG9jYWwuc2VsZjAeFw0yMzAzMjgwMzQ1MDlaFw0yNDAz | ||
MjcwMzQ1MDlaMBUxEzARBgNVBAMMCmxvY2FsLnNlbGYwggEiMA0GCSqGSIb3DQEB | ||
AQUAA4IBDwAwggEKAoIBAQDIdnZmgBgAGOChpZGwikpQgTRTkqFNKQV5jRQXr7r7 | ||
3xV+PilkDShz+UWfYG3zszLX7L9jllIDj1V5YALV+f3tjDaPnptSUnNIKIdjripQ | ||
ojX5639oCmHA4ZnYbDxx8GcNlfOLGW4oAVuY9PHaL69nmrHq58wQX2VPR6jCjzCY | ||
T0putkMCRbCTbzeb0ntcEHNKrVuk8TlLxGnNNXyeF1BS6YHa9/3PwXmwsApZ1fY2 | ||
KepHclqVISz+E0Cj4KkVvGI++9KBBDcNMEyerhU0ocotXoqQxQKm+eeED44LKnnh | ||
czPION+xRTfsqn+j8AyrShY+JkBt+VseOlOeNpn25p5dAgMBAAGjajBoMB0GA1Ud | ||
DgQWBBQ8aBcGhCLZzEGztBxgChe+zIY88TAfBgNVHSMEGDAWgBQ8aBcGhCLZzEGz | ||
tBxgChe+zIY88TAPBgNVHRMBAf8EBTADAQH/MBUGA1UdEQQOMAyCCmxvY2FsLnNl | ||
bGYwDQYJKoZIhvcNAQELBQADggEBAHBKNQHNBlRdI6cICeEkBYpoJRg1UBCEpxPo | ||
A7He5EN1vZReJcMoFoc86tPsvUaIwsgqiEu6S0sQNahJHKF0FwcB+A6F9kQmW7si | ||
CrQw9hHneooEmYs2CldNV4w51HWKNDZ5Ra+gH5B2AKR1EWFDh4MMzPlL2MPgasFC | ||
OeAOFrftD8hLLSvCvDsIall/pOg6wTP5vimndsjw+fPk7/SRAqZZzM1EQ4WZ7uZq | ||
oRXdSOhtfyEZYsC04VQNT0KAD+m73ctYXn4EHRpHx5tA1ZkKuuOLOXSVYmHRkk72 | ||
ReutF5Khp+XIjc77fHX6KRCWxT/KE23a4aEmmJzKhV3A1bcvpQ4= | ||
-----END CERTIFICATE----- |
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 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDIdnZmgBgAGOCh | ||
pZGwikpQgTRTkqFNKQV5jRQXr7r73xV+PilkDShz+UWfYG3zszLX7L9jllIDj1V5 | ||
YALV+f3tjDaPnptSUnNIKIdjripQojX5639oCmHA4ZnYbDxx8GcNlfOLGW4oAVuY | ||
9PHaL69nmrHq58wQX2VPR6jCjzCYT0putkMCRbCTbzeb0ntcEHNKrVuk8TlLxGnN | ||
NXyeF1BS6YHa9/3PwXmwsApZ1fY2KepHclqVISz+E0Cj4KkVvGI++9KBBDcNMEye | ||
rhU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FvmfDxK | ||
Q1PhfbMquHCLWrOpl1JpmRKJin555wL7EgyEFlLGs35AfGS589ofjz8+YxTRd6I4 | ||
c9Z0Ba+OVRCVo/YAwzWXd4d+/7VqykfdtRoUWMShAoGAVamX3xdVKVUH1vkbcW47 | ||
MZMvli5qeWFKkjNYzAIkHCOKgP+LcOhj2yywSliXZjBP3/AM4IyTf85rL7WWUHs9 | ||
M9V6iy4s0v95+NQ3gBhU8dqGZMlMAfHTdOYP5QZWKQQfF0iGHDH6faEsdBBWiqhI | ||
laxSnUjUN5VMS/ViBExyBQs= | ||
-----END PRIVATE KEY----- |
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,32 @@ | ||
package examples | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
// Example about sending Authentication header | ||
func TestAuth(t *testing.T) { | ||
var curlCmdExecuted string | ||
ts := createEchoServer() | ||
defer ts.Close() | ||
// Test authentication usernae and password | ||
client := resty.New() | ||
resp, err := client.R(). | ||
SetBasicAuth("USER", "PASSWORD"). | ||
SetResultCurlCmd(&curlCmdExecuted). | ||
Get( ts.URL+"/echo",) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !strings.Contains(curlCmdExecuted, "Authorization: Basic ") { | ||
t.Fatal("bad curl:", curlCmdExecuted) | ||
} | ||
if !strings.Contains(string(resp.Body()), "Authorization: Basic ") { | ||
t.Fatal("bad auth body:\n" + resp.String()) | ||
} | ||
t.Log(curlCmdExecuted) | ||
} |
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,120 @@ | ||
package examples | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptrace" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
// Example about cancel request with context | ||
func TestSetContextCancelMulti(t *testing.T) { | ||
// 0. Init test server | ||
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(2 * time.Microsecond) | ||
n, err := w.Write([]byte("TestSetContextCancel: response")) | ||
t.Logf("%s Server: wrote %d bytes", time.Now(), n) | ||
t.Logf("%s Server: err is %v ", time.Now(), err) | ||
}, 0) | ||
defer ts.Close() | ||
|
||
// 1. Create client | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
client := resty.New().R().SetContext(ctx) | ||
go func() { | ||
time.Sleep(1 * time.Microsecond) | ||
cancel() | ||
}() | ||
|
||
// 2. First request | ||
_, err := client.Get(ts.URL + "/get") | ||
if !errIsContextCancel(err) { | ||
t.Fatalf("Got unexpected error: %v", err) | ||
} | ||
|
||
// 3. Second request | ||
_, err = client.Get(ts.URL + "/get") | ||
if !errIsContextCancel(err) { | ||
t.Fatalf("Got unexpected error: %v", err) | ||
} | ||
} | ||
|
||
// Test context: cancel with chan | ||
func TestSetContextCancelWithChan(t *testing.T) { | ||
ch := make(chan struct{}) | ||
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) { | ||
defer func() { | ||
ch <- struct{}{} // tell test request is finished | ||
}() | ||
t.Logf("%s Server: %v %v", time.Now(), r.Method, r.URL.Path) | ||
ch <- struct{}{} // tell test request is canceld | ||
t.Logf("%s Server: call canceld", time.Now()) | ||
|
||
<-ch // wait for client to finish request | ||
n, err := w.Write([]byte("TestSetContextCancel: response")) | ||
// FIXME? test server doesn't handle request cancellation | ||
t.Logf("%s Server: wrote %d bytes", time.Now(), n) | ||
t.Logf("%s Server: err is %v ", time.Now(), err) | ||
|
||
}, 0) | ||
defer ts.Close() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
go func() { | ||
<-ch // wait for server to start request handling | ||
cancel() | ||
}() | ||
|
||
_, err := resty.New().R().SetContext(ctx).Get(ts.URL + "/get") | ||
t.Logf("%s:client:is canceled", time.Now()) | ||
|
||
ch <- struct{}{} // tell server to continue request handling | ||
t.Logf("%s:client:tell server to continue", time.Now()) | ||
|
||
<-ch // wait for server to finish request handling | ||
|
||
if !errIsContextCancel(err) { | ||
t.Fatalf("Got unexpected error: %v", err) | ||
} | ||
} | ||
|
||
// test with trace context | ||
func TestContextWithTrace(t *testing.T) { | ||
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("TestSetContextWithTrace: response")) | ||
}, 0) | ||
defer ts.Close() | ||
|
||
//1. Create Trace context | ||
traceInfo := struct { | ||
dnsDone time.Time | ||
connectDone time.Time | ||
}{} | ||
|
||
trace := &httptrace.ClientTrace{ | ||
ConnectStart: func(network, addr string) { | ||
traceInfo.dnsDone = time.Now() | ||
t.Log(time.Now(), "ConnectStart:", "network=", network, ",addr=", addr) | ||
}, | ||
ConnectDone: func(network, addr string, err error) { | ||
traceInfo.connectDone = time.Now() | ||
t.Log(time.Now(), "ConnectDone:", "network=", network, ",addr=", addr) | ||
}, | ||
} | ||
ctx := httptrace.WithClientTrace(context.Background(), trace) | ||
|
||
//2. Send request with Trace context | ||
session := resty.New().R().SetContext(ctx) | ||
params := MapString{"name": "ahuigo", "page": "1"} | ||
_, err := session.SetQueryParams(params).Get(ts.URL+"/get") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if traceInfo.connectDone.Sub(traceInfo.dnsDone) <= 0 { | ||
t.Fatal("Bad trace info") | ||
} | ||
|
||
} |
Oops, something went wrong.