Skip to content

Commit 285e80c

Browse files
committed
fix: avoid debug trace if http.Request.Body is nil
The govmomi soap and rest clients ensure Request.Body is not nil, but custom clients may not.
1 parent dde5090 commit 285e80c

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

vim25/debug/debug_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (c) 2022 VMware, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package debug_test
18+
19+
import (
20+
"context"
21+
"net/http"
22+
"sync"
23+
"testing"
24+
25+
"github.com/vmware/govmomi/find"
26+
"github.com/vmware/govmomi/simulator"
27+
"github.com/vmware/govmomi/vapi/rest"
28+
"github.com/vmware/govmomi/vim25"
29+
"github.com/vmware/govmomi/vim25/debug"
30+
31+
_ "github.com/vmware/govmomi/vapi/simulator"
32+
)
33+
34+
func TestSetProvider(t *testing.T) {
35+
p := debug.FileProvider{
36+
Path: t.TempDir(),
37+
}
38+
debug.SetProvider(&p)
39+
40+
simulator.Test(func(ctx context.Context, c *vim25.Client) {
41+
var wg sync.WaitGroup
42+
rc := rest.NewClient(c)
43+
44+
// hit the debug package with some concurrency (see PR #2469)
45+
for i := 0; i < 5; i++ {
46+
wg.Add(1)
47+
go func() {
48+
defer wg.Done()
49+
finder := find.NewFinder(c)
50+
51+
_, err := finder.VirtualMachineList(ctx, "*")
52+
if err != nil {
53+
t.Error(err)
54+
}
55+
}()
56+
}
57+
58+
wg.Wait()
59+
60+
// send an http request with a nil Body to ensure debug trace doesn't panic in this case
61+
u := rc.URL().String() + "/com/vmware/cis/session"
62+
63+
req, err := http.NewRequest(http.MethodPost, u, nil)
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
req.SetBasicAuth("user", "pass")
69+
var id string
70+
if err = rc.Do(ctx, req, &id); err != nil {
71+
t.Fatal(err)
72+
}
73+
})
74+
}

vim25/soap/debug.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ func (d *debugRoundTrip) debugRequest(req *http.Request) string {
8787
ext := d.ext(req.Header)
8888
// Capture body
8989
wc = d.newFile("req." + ext)
90-
req.Body = Trace(req.Body, wc, ext)
90+
if req.Body != nil {
91+
req.Body = Trace(req.Body, wc, ext)
92+
}
9193

9294
// Delay closing until marked done
9395
d.cs = append(d.cs, wc)

0 commit comments

Comments
 (0)