mirror of
https://github.com/go-i2p/goSam.git
synced 2025-06-10 09:40:48 -04:00
small cleanup after playing with i2pd in c++
This commit is contained in:
@ -24,7 +24,10 @@ func NewClient(addr string) (*Client, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c := &Client{conn, false}
|
c := &Client{
|
||||||
|
SamConn: conn,
|
||||||
|
verbose: false,
|
||||||
|
}
|
||||||
return c, c.hello()
|
return c, c.hello()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,27 +6,25 @@ var (
|
|||||||
client *Client
|
client *Client
|
||||||
)
|
)
|
||||||
|
|
||||||
func setup() {
|
func setup(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// these tests expect a running SAM brige on this address
|
// these tests expect a running SAM brige on this address
|
||||||
client, err = NewDefaultClient()
|
client, err = NewDefaultClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
t.Fatalf("NewDefaultClient() Error: %q\n", err)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func teardown() {
|
//client.ToggleVerbose()
|
||||||
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) {
|
func TestClientHello(t *testing.T) {
|
||||||
var err error
|
setup(t)
|
||||||
|
teardown(t)
|
||||||
client, err = NewDefaultClient()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("client.Hello() should not throw an error.\n%s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
client.Close()
|
|
||||||
}
|
}
|
||||||
|
50
example/httpTest.go
Normal file
50
example/httpTest.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -8,8 +8,8 @@ import (
|
|||||||
func TestClientLookupInvalid(t *testing.T) {
|
func TestClientLookupInvalid(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
setup()
|
setup(t)
|
||||||
defer teardown()
|
defer teardown(t)
|
||||||
|
|
||||||
addr, err := client.Lookup("abci2p")
|
addr, err := client.Lookup("abci2p")
|
||||||
if addr != "" || err == nil {
|
if addr != "" || err == nil {
|
||||||
@ -17,20 +17,25 @@ func TestClientLookupInvalid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
repErr, ok := err.(ReplyError)
|
repErr, ok := err.(ReplyError)
|
||||||
if ok && repErr.Result != ResultKeyNotFound {
|
if !ok {
|
||||||
t.Error("client.Lookup() should throw an ResultKeyNotFound error. Got:%v\n", repErr)
|
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() {
|
func ExampleClient_Lookup() {
|
||||||
var err error
|
client, err := NewDefaultClient()
|
||||||
|
if err != nil {
|
||||||
setup()
|
fmt.Printf("NewDefaultClient() should not throw an error.\n%s\n", err)
|
||||||
defer teardown()
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, err = client.Lookup("zzz.i2p")
|
_, err = client.Lookup("zzz.i2p")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("client.Lookup() should not throw an error.\n%s\n", err)
|
fmt.Printf("client.Lookup() should not throw an error.\n%s\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Address of zzz.i2p:")
|
fmt.Println("Address of zzz.i2p:")
|
||||||
|
Reference in New Issue
Block a user