small cleanup after playing with i2pd in c++

This commit is contained in:
Henry
2014-10-31 10:52:33 +01:00
parent 97f9bc7e56
commit 8badb1ef70
4 changed files with 77 additions and 21 deletions

View File

@ -24,7 +24,10 @@ func NewClient(addr string) (*Client, error) {
if err != nil {
return nil, err
}
c := &Client{conn, false}
c := &Client{
SamConn: conn,
verbose: false,
}
return c, c.hello()
}

View File

@ -6,27 +6,25 @@ var (
client *Client
)
func setup() {
func setup(t *testing.T) {
var err error
// these tests expect a running SAM brige on this address
client, err = NewDefaultClient()
if err != nil {
panic(err)
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
//client.ToggleVerbose()
}
func teardown() {
client.Close()
func teardown(t *testing.T) {
if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err)
}
}
func TestClientHello(t *testing.T) {
var err error
client, err = NewDefaultClient()
if err != nil {
t.Errorf("client.Hello() should not throw an error.\n%s\n", err)
}
client.Close()
setup(t)
teardown(t)
}

50
example/httpTest.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"io"
"log"
"net/http"
"os"
"github.com/cryptix/goSam"
)
func main() {
// create a default sam client
sam, err := goSam.NewDefaultClient()
checkErr(err)
log.Println("Client Created")
// create a transport that uses SAM to dial TCP Connections
tr := &http.Transport{
Dial: sam.Dial,
}
// create a client using this transport
client := &http.Client{Transport: tr}
// send a get request
resp, err := client.Get("http://stats.i2p/")
checkErr(err)
defer resp.Body.Close()
log.Printf("Get returned %+v\n", resp)
// create a file for the response
file, err := os.Create("stats.html")
checkErr(err)
defer file.Close()
// copy the response to the file
_, err = io.Copy(file, resp.Body)
checkErr(err)
log.Println("Done.")
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}

View File

@ -8,8 +8,8 @@ import (
func TestClientLookupInvalid(t *testing.T) {
var err error
setup()
defer teardown()
setup(t)
defer teardown(t)
addr, err := client.Lookup("abci2p")
if addr != "" || err == nil {
@ -17,20 +17,25 @@ func TestClientLookupInvalid(t *testing.T) {
}
repErr, ok := err.(ReplyError)
if ok && repErr.Result != ResultKeyNotFound {
t.Error("client.Lookup() should throw an ResultKeyNotFound error. Got:%v\n", repErr)
if !ok {
t.Fatalf("client.Lookup() should return a ReplyError")
}
if repErr.Result != ResultKeyNotFound {
t.Errorf("client.Lookup() should throw an ResultKeyNotFound error.\nGot:%+v\n", repErr)
}
}
func ExampleClient_Lookup() {
var err error
setup()
defer teardown()
client, err := NewDefaultClient()
if err != nil {
fmt.Printf("NewDefaultClient() should not throw an error.\n%s\n", err)
return
}
_, err = client.Lookup("zzz.i2p")
if err != nil {
fmt.Printf("client.Lookup() should not throw an error.\n%s\n", err)
return
}
fmt.Println("Address of zzz.i2p:")