Skip to content

Commit

Permalink
simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
powerman committed Sep 7, 2015
1 parent e40d686 commit d3b4477
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
44 changes: 0 additions & 44 deletions jsonrpc2/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
package jsonrpc2

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/rpc"
"strings"
"testing"
Expand Down Expand Up @@ -251,47 +248,6 @@ func TestUnexpectedError(t *testing.T) {
ServeConn(srv) // must return, not loop
}

func TestBadHTTP2Server(t *testing.T) {
ts := httptest.NewServer(HTTPHandler(nil))
// Don't close because of https://github.com/golang/go/issues/12262
// defer ts.Close()
addr := ts.URL[strings.LastIndex(ts.URL, "/")+1:]
for _, c := range []string{"", " ", "{", `{"jsonrpc":"2.0",`} {
conn, err := net.Dial("tcp", addr)
if err != nil {
t.Fatalf("Dial(%s), err = %v", addr, err)
}
_, err = conn.Write([]byte("POST / HTTP/1.0\r\n" +
"Host: localhost\r\n" +
"Content-Type: application/json\r\n" +
"Accept: application/json\r\n" +
"Content-Length: " + fmt.Sprintf("%d", len(c)) + "\r\n" +
"\r\n" + c))
if err != nil {
t.Fatalf("[%#q] Write(), err = %v", c, err)
}
req, err := http.NewRequest("POST", ts.URL, strings.NewReader(c))
if err != nil {
t.Errorf("[%#q] NewRequest(), err = %v", c, err)
}
resp, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
t.Fatalf("[%#q] ReadResponse(), err = %v", c, err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("[%#q] resp.StatusCode = %d, want %d", c, resp.StatusCode, http.StatusOK)
}
var r clientResponse
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
t.Errorf("[%#q] Decode(), err = %v", c, err)
}
if r.Error == nil || r.Error.Code != errParse.Code {
t.Errorf("[%#q] r = %v, wait errParse", c, r)
}
}
}

// Copied from package net.
func myPipe() (*pipe, *pipe) {
r1, w1 := io.Pipe()
Expand Down
5 changes: 5 additions & 0 deletions jsonrpc2/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestHTTPServer(t *testing.T) {
const jNotify = `{"jsonrpc":"2.0","method":"Svc.Sum","params":[3,5]}`
const jRes = `{"jsonrpc":"2.0","id":0,"result":8}`
const jErr = `{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}}`
const jParse = `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`
const contentType = "application/json"

cases := []struct {
Expand All @@ -49,6 +50,10 @@ func TestHTTPServer(t *testing.T) {
{"POST", contentType, contentType, jNotify, http.StatusNoContent, ""},
{"POST", contentType, contentType, jSum, http.StatusOK, jRes},
{"POST", contentType, contentType, jBad, http.StatusOK, jErr},
{"POST", contentType, contentType, "", http.StatusOK, jParse},
{"POST", contentType, contentType, " ", http.StatusOK, jParse},
{"POST", contentType, contentType, "{", http.StatusOK, jParse},
{"POST", contentType, contentType, `{"jsonrpc":"2.0",`, http.StatusOK, jParse},
}

ts := httptest.NewServer(jsonrpc2.HTTPHandler(nil))
Expand Down

0 comments on commit d3b4477

Please sign in to comment.