@@ -7,6 +7,7 @@ package client
77import (
88 "fmt"
99 "io/ioutil"
10+ "net"
1011 "net/http"
1112 "os"
1213 "path"
@@ -93,7 +94,13 @@ func TestClient(t *testing.T) {
9394 http .HandleFunc ("/golang.org/example/one.json" , serveTestVuln1 )
9495 http .HandleFunc ("/golang.org/example/two.json" , serveTestVuln2 )
9596 http .HandleFunc ("/index.json" , serveIndex )
96- go func () { http .ListenAndServe (":8080" , nil ) }()
97+
98+ l , err := net .Listen ("tcp" , "127.0.0.1:" )
99+ if err != nil {
100+ t .Fatalf ("failed to listen on 127.0.0.1: %s" , err )
101+ }
102+ _ , port , _ := net .SplitHostPort (l .Addr ().String ())
103+ go func () { http .Serve (l , nil ) }()
97104
98105 // Create a local file database.
99106 localDBName , err := localDB (t )
@@ -110,11 +117,11 @@ func TestClient(t *testing.T) {
110117 summaries map [string ]string
111118 }{
112119 // Test the http client without any cache.
113- {name : "http-no-cache" , source : "http://localhost:8080" , createCache : func () Cache { return nil }, noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "" }},
120+ {name : "http-no-cache" , source : "http://localhost:" + port , createCache : func () Cache { return nil }, noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "" }},
114121 // Test the http client with empty cache.
115- {name : "http-empty-cache" , source : "http://localhost:8080" , createCache : func () Cache { return & fsCache {} }, noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "" }},
122+ {name : "http-empty-cache" , source : "http://localhost:" + port , createCache : func () Cache { return & fsCache {} }, noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "" }},
116123 // Test the client with non-stale cache containing a version of testVuln2 where Summary="cached".
117- {name : "http-cache" , source : "http://localhost:8080" , createCache : cachedTestVuln2 ("localhost" ), noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "cached" }},
124+ {name : "http-cache" , source : "http://localhost:" + port , createCache : cachedTestVuln2 ("localhost" ), noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "cached" }},
118125 // Repeat the same for local file client.
119126 {name : "file-no-cache" , source : "file://" + localDBName , createCache : func () Cache { return nil }, noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "" }},
120127 {name : "file-empty-cache" , source : "file://" + localDBName , createCache : func () Cache { return & fsCache {} }, noVulns : 2 , summaries : map [string ]string {"ID1" : "" , "ID2" : "" }},
0 commit comments