This repository was archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
interoptestservice: support GET and POST HTTP requests #115
Open
odeke-em
wants to merge
1
commit into
master
Choose a base branch
from
interoptestservice-http-GET+POST
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
interoptest/src/testcoordinator/interoptestservice/interop_test_service_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright 2018, OpenCensus Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package interoptestservice_test | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/http/httputil" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/census-ecosystem/opencensus-experiments/interoptest/src/testcoordinator/interoptestservice" | ||
| ) | ||
|
|
||
| func TestRequestsOverHTTP(t *testing.T) { | ||
| h := new(interoptestservice.ServiceImpl) | ||
| tests := []struct { | ||
| reqWire string // The request's wire data | ||
| wantResWire string // The response's wire format | ||
| }{ | ||
|
|
||
| // OPTIONS request | ||
| { | ||
| reqWire: `OPTIONS / HTTP/1.1 | ||
| Host: * | ||
|
|
||
| `, | ||
| wantResWire: "HTTP/1.1 200 OK\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Access-Control-Allow-Headers: *\r\n" + | ||
| "Access-Control-Allow-Methods: *\r\n" + | ||
| "Access-Control-Allow-Origin: *\r\n\r\n", | ||
| }, | ||
|
|
||
| // GET: bad path | ||
| { | ||
| reqWire: `GET / HTTP/1.1 | ||
| Host: foo | ||
| Content-Length: 0 | ||
|
|
||
| `, | ||
| wantResWire: "HTTP/1.1 400 Bad Request\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: text/plain; charset=utf-8\r\n" + | ||
| "X-Content-Type-Options: nosniff\r\n\r\n" + | ||
| "Expected path of the form: /result/:id\n", | ||
| }, | ||
|
|
||
| // GET: good path no id | ||
| { | ||
| reqWire: `GET /result HTTP/1.1 | ||
|
|
||
| `, | ||
| wantResWire: "HTTP/1.1 400 Bad Request\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: text/plain; charset=utf-8\r\n" + | ||
| "X-Content-Type-Options: nosniff\r\n\r\n" + | ||
| "Expected path of the form: /result/:id\n", | ||
| }, | ||
|
|
||
| // GET: good path with proper id | ||
| { | ||
| reqWire: `GET /result/1 HTTP/1.1 | ||
|
|
||
| `, | ||
| wantResWire: "HTTP/1.1 200 OK\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: application/json\r\n\r\n" + | ||
| `{"id":1,"status":{}}`, | ||
| }, | ||
| // POST: no body | ||
| { | ||
| reqWire: `POST /result HTTP/1.1 | ||
|
|
||
| `, | ||
| wantResWire: "HTTP/1.1 400 Bad Request\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: text/plain; charset=utf-8\r\n" + | ||
| "X-Content-Type-Options: nosniff\r\n\r\n" + | ||
| "Failed to JSON unmarshal interop.InteropResultRequest: unexpected end of JSON input\n", | ||
| }, | ||
|
|
||
| // POST: body with content length to accepted route | ||
| { | ||
| reqWire: `POST /result HTTP/1.1 | ||
| Content-Length: 9 | ||
| Content-Type: application/json | ||
|
|
||
| {"id":10} | ||
| `, | ||
| wantResWire: "HTTP/1.1 200 OK\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: application/json\r\n\r\n" + | ||
| `{"id":10,"status":{}}`, | ||
| }, | ||
|
|
||
| // POST: body with no content length | ||
| { | ||
| // Using a string concatenation here because for "streamed"/"chunked" | ||
| // requests, we have to ensure that the last 2 bytes before EOF are | ||
| // strictly "\r\n" lest a "malformed chunked encoding" error. | ||
| reqWire: "POST /result HTTP/1.1\r\n" + | ||
| "Host: golang.org\r\n" + | ||
| "Content-Type: application/json\r\n" + | ||
| "Transfer-Encoding: chunked\r\n" + | ||
| "Accept-Encoding: gzip\r\n\r\n" + | ||
| "b\r\n" + | ||
| "{\"id\":8888}\r\n" + | ||
| "0\r\n\r\n", | ||
| wantResWire: "HTTP/1.1 200 OK\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: application/json\r\n\r\n" + | ||
| `{"id":8888,"status":{}}`, | ||
| }, | ||
|
|
||
| // POST: body with content length to non-existent route | ||
| { | ||
| reqWire: `POST /results HTTP/1.1 | ||
| Content-Length: 9 | ||
| Content-Type: application/json | ||
|
|
||
| {"id":10} | ||
| `, | ||
| wantResWire: "HTTP/1.1 404 Not Found\r\n" + | ||
| "Connection: close\r\n" + | ||
| "Content-Type: text/plain; charset=utf-8\r\n" + | ||
| "X-Content-Type-Options: nosniff\r\n\r\n" + | ||
| "Unmatched route: /results\n" + | ||
| "Only accepting /result and /run\n", | ||
| }, | ||
| } | ||
|
|
||
| for i, tt := range tests { | ||
| req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(tt.reqWire))) | ||
| if err != nil { | ||
| t.Errorf("#%d unexpected error parsing request: %v", i, err) | ||
| continue | ||
| } | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| h.ServeHTTP(rec, req) | ||
| gotResBlob, _ := httputil.DumpResponse(rec.Result(), true) | ||
| gotRes := string(gotResBlob) | ||
| if gotRes != tt.wantResWire { | ||
| t.Errorf("#%d non-matching responses\nGot:\n%q\nWant:\n%q", i, gotRes, tt.wantResWire) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the difference between POST /result and GET /result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and as per the PR there are two different methods handleHTTPGET and handleHTTPPOST
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant was GET /result and POST /result do the same thing. They both retrieve result. Why is there a need for POST /result ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am a little confused by your the ask of this issue then: A POST method is used to upload content, a GET is used to retrieve it. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods my apologies if am perhaps preaching to the choir.
POST was coded to be able to receive test results from whoever is running it e.g. a sharded test runner; GET to actually retrieve them(following the HTTP conventions)
Did you just want only functionality of a GET?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test coordinator is computing results based on test it requests to test-services (javaservice, etc) and traces it receives from OC Agent. So no entity is actually posting any result to Test coordinator.
Also, the code for POST /result is actually reading the request from the body and returning the results. It isn't uploading any content.