File: json_spec.rb

package info (click to toggle)
ruby-webmock 3.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,905; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 1,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'spec_helper'

describe WebMock::Util::Parsers::JSON do
  describe ".parse" do
    it "should parse json without parsing dates" do
      expect(described_class.parse("\"a\":\"2011-01-01\"")).to eq(
        {"a" => "2011-01-01"}
      )
    end

    it "can parse json with multibyte characters" do
      expect(described_class.parse(
        "{\"name\":\"山田太郎\"\,\"job\":\"会社員\"}"
      )).to eq({"name" => "山田太郎", "job" => "会社員"})
    end

    it "rescues ArgumentError's from YAML.load" do
      allow(YAML).to receive(:load).and_raise(ArgumentError)
      expect {
        described_class.parse("Bad JSON")
      }.to raise_error WebMock::Util::Parsers::ParseError
    end

    it "rescues Psych::SyntaxError's from YAML.load" do
      allow(YAML).to receive(:load).and_raise(Psych::SyntaxError)
      expect {
        described_class.parse("Bad JSON")
      }.to raise_error WebMock::Util::Parsers::ParseError
    end
  end

  describe ".convert_json_to_yaml" do
    it "parses multibyte characters" do
      expect(described_class.convert_json_to_yaml(
        "{\"name\":\"山田太郎\"\,\"job\":\"会社員\"}"
      )).to eq "{\"name\": \"山田太郎\", \"job\": \"会社員\"}"
    end
  end
end