Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/FSharp.Data.Core.Tests/FSharp.Data.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<Compile Include="Http.fs" />
<Compile Include="HttpContentTypes.fs" />
<Compile Include="HttpRequestHeaders.fs" />
<Compile Include="NameUtils.fs" />
<Compile Include="Pluralizer.fs" />
Expand All @@ -29,6 +30,7 @@
<Compile Include="JsonValue.fs" />
<Compile Include="JsonParserProperties.fs" />
<Compile Include="JsonConversions.fs" />
<Compile Include="JsonDocument.fs" />
<Compile Include="JsonRuntime.fs" />
<Compile Include="JsonSchema.fs" />
<Compile Include="CsvReader.fs" />
Expand Down
73 changes: 73 additions & 0 deletions tests/FSharp.Data.Core.Tests/HttpContentTypes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module FSharp.Data.Tests.HttpContentTypes

open NUnit.Framework
open FsUnit
open FSharp.Data

[<Test>]
let ``HttpContentTypes.Any should return correct MIME type`` () =
HttpContentTypes.Any |> should equal "*/*"

[<Test>]
let ``HttpContentTypes.Text should return correct MIME type`` () =
HttpContentTypes.Text |> should equal "text/plain"

[<Test>]
let ``HttpContentTypes.Binary should return correct MIME type`` () =
HttpContentTypes.Binary |> should equal "application/octet-stream"

[<Test>]
let ``HttpContentTypes.Zip should return correct MIME type`` () =
HttpContentTypes.Zip |> should equal "application/zip"

[<Test>]
let ``HttpContentTypes.GZip should return correct MIME type`` () =
HttpContentTypes.GZip |> should equal "application/gzip"

[<Test>]
let ``HttpContentTypes.FormValues should return correct MIME type`` () =
HttpContentTypes.FormValues |> should equal "application/x-www-form-urlencoded"

[<Test>]
let ``HttpContentTypes.Json should return correct MIME type`` () =
HttpContentTypes.Json |> should equal "application/json"

[<Test>]
let ``HttpContentTypes.JavaScript should return correct MIME type`` () =
HttpContentTypes.JavaScript |> should equal "application/javascript"

[<Test>]
let ``HttpContentTypes.Xml should return correct MIME type`` () =
HttpContentTypes.Xml |> should equal "application/xml"

[<Test>]
let ``HttpContentTypes.Rss should return correct MIME type`` () =
HttpContentTypes.Rss |> should equal "application/rss+xml"

[<Test>]
let ``HttpContentTypes.Atom should return correct MIME type`` () =
HttpContentTypes.Atom |> should equal "application/atom+xml"

[<Test>]
let ``HttpContentTypes.Rdf should return correct MIME type`` () =
HttpContentTypes.Rdf |> should equal "application/rdf+xml"

[<Test>]
let ``HttpContentTypes.Html should return correct MIME type`` () =
HttpContentTypes.Html |> should equal "text/html"

[<Test>]
let ``HttpContentTypes.XHtml should return correct MIME type`` () =
HttpContentTypes.XHtml |> should equal "application/xhtml+xml"

[<Test>]
let ``HttpContentTypes.Soap should return correct MIME type`` () =
HttpContentTypes.Soap |> should equal "application/soap+xml"

[<Test>]
let ``HttpContentTypes.Csv should return correct MIME type`` () =
HttpContentTypes.Csv |> should equal "text/csv"

[<Test>]
let ``HttpContentTypes.JsonRpc should return correct MIME type`` () =
HttpContentTypes.JsonRpc |> should equal "application/json-rpc"
122 changes: 122 additions & 0 deletions tests/FSharp.Data.Core.Tests/JsonDocument.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module FSharp.Data.Tests.JsonDocument

open NUnit.Framework
open FsUnit
open FSharp.Data
open FSharp.Data.Runtime.BaseTypes
open System.IO
open System.Reflection

// Use reflection to access the "generated code only" methods for testing
let private getCreateMethod() =
typeof<JsonDocument>.GetMethod("Create", [| typeof<JsonValue>; typeof<string> |])

let private getCreateFromReaderMethod() =
typeof<JsonDocument>.GetMethod("Create", [| typeof<System.IO.TextReader> |])

let private getCreateListMethod() =
typeof<JsonDocument>.GetMethod("CreateList", [| typeof<System.IO.TextReader> |])

[<Test>]
let ``JsonDocument.Create with JsonValue should return IJsonDocument using reflection`` () =
let createMethod = getCreateMethod()
let jsonValue = JsonValue.Number 42M
let doc = createMethod.Invoke(null, [| jsonValue; "/path" |]) :?> IJsonDocument

doc |> should not' (be null)
doc.JsonValue |> should equal jsonValue

[<Test>]
let ``JsonDocument.Create with TextReader should parse JSON using reflection`` () =
let createMethod = getCreateFromReaderMethod()
let json = """{"name": "test", "value": 123}"""
use reader = new StringReader(json)
let doc = createMethod.Invoke(null, [| reader |]) :?> IJsonDocument

doc |> should not' (be null)
doc.JsonValue |> should not' (be null)

[<Test>]
let ``JsonDocument.CreateList with single array should return array elements using reflection`` () =
let createListMethod = getCreateListMethod()
let json = """[{"id": 1}, {"id": 2}]"""
use reader = new StringReader(json)
let docs = createListMethod.Invoke(null, [| reader |]) :?> IJsonDocument[]

docs |> should haveLength 2
docs.[0].JsonValue.["id"].AsInteger() |> should equal 1
docs.[1].JsonValue.["id"].AsInteger() |> should equal 2

[<Test>]
let ``JsonDocument.CreateList with multiple JSON objects should return separate documents using reflection`` () =
let createListMethod = getCreateListMethod()
let json = """{"id": 1}{"id": 2}"""
use reader = new StringReader(json)
let docs = createListMethod.Invoke(null, [| reader |]) :?> IJsonDocument[]

docs |> should haveLength 2
docs.[0].JsonValue.["id"].AsInteger() |> should equal 1
docs.[1].JsonValue.["id"].AsInteger() |> should equal 2

[<Test>]
let ``JsonDocument ToString should return JsonValue string representation using reflection`` () =
let createMethod = getCreateMethod()
let jsonValue = JsonValue.Number 42M
let docObj = createMethod.Invoke(null, [| jsonValue; "/test" |])

docObj.ToString() |> should equal "42"

[<Test>]
let ``JsonDocument JsonValue property should return original JsonValue using reflection`` () =
let createMethod = getCreateMethod()
let jsonValue = JsonValue.String "test"
let doc = createMethod.Invoke(null, [| jsonValue; "/test" |]) :?> IJsonDocument

doc.JsonValue |> should equal jsonValue

[<Test>]
let ``IJsonDocument Path method should return path using reflection`` () =
let createMethod = getCreateMethod()
let jsonValue = JsonValue.Boolean true
let doc = createMethod.Invoke(null, [| jsonValue; "/root/item" |]) :?> IJsonDocument

// Use reflection to call the Path method to avoid the compiler message
let pathMethod = typeof<IJsonDocument>.GetMethod("Path")
let path = pathMethod.Invoke(doc, [||]) :?> string

path |> should equal "/root/item"

[<Test>]
let ``IJsonDocument CreateNew should create new document with incremented path using reflection`` () =
let createMethod = getCreateMethod()
let jsonValue = JsonValue.Array [| JsonValue.Number 1M; JsonValue.Number 2M |]
let originalDoc = createMethod.Invoke(null, [| jsonValue; "/root" |]) :?> IJsonDocument
let newValue = JsonValue.Number 42M

// Use reflection to call CreateNew to avoid the compiler message
let createNewMethod = typeof<IJsonDocument>.GetMethod("CreateNew")
let newDoc = createNewMethod.Invoke(originalDoc, [| newValue; "/item[0]" |]) :?> IJsonDocument

newDoc.JsonValue |> should equal newValue
let pathMethod = typeof<IJsonDocument>.GetMethod("Path")
let path = pathMethod.Invoke(newDoc, [||]) :?> string
path |> should equal "/root/item[0]"

[<Test>]
let ``JsonDocument.Create with empty JSON should work using reflection`` () =
let createMethod = getCreateFromReaderMethod()
let json = "{}"
use reader = new StringReader(json)
let doc = createMethod.Invoke(null, [| reader |]) :?> IJsonDocument

doc |> should not' (be null)
doc.JsonValue |> should not' (be null)

[<Test>]
let ``JsonDocument.CreateList with empty array should return empty array using reflection`` () =
let createListMethod = getCreateListMethod()
let json = "[]"
use reader = new StringReader(json)
let docs = createListMethod.Invoke(null, [| reader |]) :?> IJsonDocument[]

docs |> should haveLength 0
Loading