Skip to content

Commit

Permalink
Add net/http/httptest package.
Browse files Browse the repository at this point in the history
  • Loading branch information
alpacaaa committed Sep 6, 2023
1 parent 8384c8c commit 87d220d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
14 changes: 11 additions & 3 deletions compiler/test/codegen-emit.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,8 @@ Net http
```rust
use fmt
use net.http
use net.http.httptest
use io
use sync

struct Counter { m: sync.Mutex, count: int }
Expand All @@ -1008,9 +1010,15 @@ impl (c: *Counter) {

fn main() {
let c = Counter { m: zeroValue(), count: 0 }
http.Handle("/", &c)
// http.ListenAndServe(":3333", zeroValue())
fmt.Println("ok")

let ts = httptest.NewServer(&c)
defer ts.Close()

let res = http.Get(ts.URL).Unwrap()
let body = io.ReadAll(res.Body).Unwrap()
res.Body.Close()

fmt.Println(string(body))
}
```

Expand Down
46 changes: 40 additions & 6 deletions compiler/test/snapshot/codegen-emit/net-http.exp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Net http
SOURCE:
use fmt
use net.http
use net.http.httptest
use io
use sync

struct Counter { m: sync.Mutex, count: int }
Expand All @@ -18,20 +20,28 @@ impl (c: *Counter) {

fn main() {
let c = Counter { m: zeroValue(), count: 0 }
http.Handle("/", &c)
// http.ListenAndServe(":3333", zeroValue())
fmt.Println("ok")

let ts = httptest.NewServer(&c)
defer ts.Close()

let res = http.Get(ts.URL).Unwrap()
let body = io.ReadAll(res.Body).Unwrap()
res.Body.Close()

fmt.Println(string(body))
}

OUTPUT:
ok
<h1>count 1</h1>

---
package main

import (
"fmt"
"io"
http "net/http"
httptest "net/http/httptest"
"sync"
)

Expand All @@ -55,7 +65,31 @@ func main() {
count: 0,
}

http.Handle("/", &c)
ts := httptest.NewServer(&c)

defer ts.Close()

var3_result := func() Result[*http.Response, error] {
var1_check, var2_err := http.Get(ts.URL)
if var2_err != nil {
return make_Result_Err[*http.Response, error](var2_err)
}
return make_Result_Ok[*http.Response, error](var1_check)
}()

res := var3_result.Unwrap()

var6_result := func() Result[[]byte, error] {
var4_check, var5_err := io.ReadAll(res.Body)
if var5_err != nil {
return make_Result_Err[[]byte, error](var5_err)
}
return make_Result_Ok[[]byte, error](var4_check)
}()

body := var6_result.Unwrap()

res.Body.Close()

fmt.Println("ok")
fmt.Println(string(body))
}
1 change: 1 addition & 0 deletions importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var REWRITE_MODULES = map[string]string{
"fs": "io.fs",
"url": "net.url",
"netip": "net.netip",
"http": "net.http",
"textproto": "net.textproto",
"multipart": "mime.multipart",
"tls": "crypto.tls",
Expand Down
4 changes: 4 additions & 0 deletions std/modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,9 @@
{
"path": "net/http",
"name": "net.http"
},
{
"path": "net/http/httptest",
"name": "net.http.httptest"
}
]
64 changes: 64 additions & 0 deletions std/net/http/httptest/httptest.brg
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use net.http
use bytes
// use crypto.x509
use net
use crypto.tls
use io


fn NewRecorder () -> *ResponseRecorder { EXT }

fn NewRequest (method: string, target: string, body: io.Reader) -> *http.Request { EXT }

fn NewServer (handler: http.Handler) -> *Server { EXT }

fn NewTLSServer (handler: http.Handler) -> *Server { EXT }

fn NewUnstartedServer (handler: http.Handler) -> *Server { EXT }

impl (self: ResponseRecorder) {

fn Flush () -> () { EXT }

fn Header () -> http.Header { EXT }

fn Result () -> *http.Response { EXT }

fn Write (buf: [byte]) -> Result<int> { EXT }

fn WriteHeader (code: int) -> () { EXT }

fn WriteString (str: string) -> Result<int> { EXT }

}

impl (self: Server) {

fn Certificate () -> *any { EXT }

fn Client () -> *http.Client { EXT }

fn Close () -> () { EXT }

fn CloseClientConnections () -> () { EXT }

fn Start () -> () { EXT }

fn StartTLS () -> () { EXT }

}

struct ResponseRecorder{
Code: int,
HeaderMap: http.Header,
Body: *bytes.Buffer,
Flushed: bool
}

struct Server{
URL: string,
Listener: net.Listener,
EnableHTTP2: bool,
TLS: *tls.Config,
Config: *http.Server
}

0 comments on commit 87d220d

Please sign in to comment.